Duration: 16 minutes
Summary: In this tutorial, we’ll continue to develop our first Struts 2 application using Eclipse tool environment and Tomcat Web Server. Introducing Packages and Action concepts, we’ll show you how to configure an application to map the Struts 2 on action. All MVC (Model, View, Controller) concepts are presented on this tutorial, providing a step by step guide.
With this course, we’re going to learn the Java Struts 2 Technology, presenting a Tomcat Web solution and exploring how a Finance Management Software can be designed using these resources.
Technology Used: Java, Apache Struts 2, Eclipse, Tomcat
Keyword: Java, Eclipse, Struts 2, Tomcat, apache, mvc
Transcript: Hello everybody, please be welcome. Let's continue our Java tutorial using struts on our web application. In the last class we have created our web.xml file, and now we're going to create a second file that must be created is the struts.xml file. This file will give us a map for the application and all the routines that we will use on the application, inside the application, all the routes, will be designed and will signed by this file. So let's create it. Inside the webLEAF source folder that I'm showing here, you will create a new file, another XML file named struts.xml. Here we go. OK.
Again, I will copy and paste some text just to show you how a simple application will work. We will use DOCTYPE arechtype to set DTD map to do XML. And after this we will have the struts tag, and inside the struts tag we will use a constant tag that defines some parameters to our application. One of them is named by struts.devmode. And when you set the struts,devmode to true you are saying that your application will give you in case of error a real big message with all this stack trace for all your error. And that will help you to find the root calls to this error. When your application is used on the production server, the online server, you may set this option to false. Because your customer will not see all this stack trace given by this constant. It helps a lot when you have some kind of trouble on the application.
But let's see how the struts file works. Struts is defined by a group of packages. And these packages must contain actions that will be used by the web application. You have the gsp files webpage, that is used as our views. And you have the actions that will be used as our model on mvc pattern. As we create a simple struts application we will design here a simple package with some actions inside, or a simple action. We have to create the package. Tag. The package tag must have a name. We will give the default name here. This package must extend struts default. And we can set a namespace to ourpackage will give /namespace. Inside the package tag we will create an action. This action must give us all the business rules. And as you must know using an mvc pattern, you will not have this rows inside your viewcode of the gsp file. We will insert these rows inside Javaclass and inside a method. So let's create the first action tag. This tag must have a name, which I will call just index. And inside our tag we must have a result. I will explain this better later. Our result will be the page or another action that will be redirected when the model code is done. And you have a result, the result may have classified, but you can just have a main result here. We will call this result the result as index.gsp.
Inside an action you can call a class to act as a a model. So we can create a class, name it indexaction here. How the map will work here. Well we must create, first of all, the index.gsp, and after that you must create a msrbl index action. Well I got the index.gsp used by our last sample, msrbl and I will use this file to create our logic. Inside the webIF source I will create a new Java class. I will call this msrbl.indexaction. Here we go. Package name msrbl and name indexaction. OK. We have a package named msrbl, our class name is indexaction, and here we will create an indexaction file. How this works on the action class. You must have a method that will be called when you call an action. You can call an action directly by the browser using an address, as we are using the local host, at the port 8080, by application name struts, test 01/index. This address will directly call the action. And when this action is called you must have a method, and have the full name as execute. And this method must return a string that will classify this execution. We'll use 'success' in our method.
All the business processes included inside this action have to come inside the main method to make it work. The next question is how do we communicate the action class with our view, our gsp file? You have a bunch of tags that you can use on this communication. The most common is called the property tag. Inside our gsp file, we must add a taglib to make struts work with the gsp file. Here you have a sample to use the taglib. You must have a prefix that will be used on every tag that uses struts. And this prefix will call the struts tag library to define how these tags will work when called. For these tags you must use the prefix. In here you've got all the tags that you can use. We will use, this first time, the property tag, and a property must have a value. You can call this value the information or something like that. Let's call it message. How will this message be used by the index action? Inside your index action you must create a property with the same name. Now you create the getter and setter for your property. And then you can set a message to this property. Like this. After this we must define, on the struts.xml, a name for the result. You can have a main name with all the name information, but let's create here the success name to give the execution to the index.gsp file. Now let's test the call. We will access the tomcat container manager APP. So here you have your struts.test right here. To have the struts.test you must create the context, the gsp work directory as we made on our last class.
Let's click on the struts test 01, and here you just have the hello world! information here. Why don't we have the welcome to java struts here? Because we are not calling the index action. To call the action you must type index here on you location. Enter. And here you've got welcome to java struts. Without this you are accessing directly to index.gsp file and calling the index. The struts.xml file defined that you have an index action that will execute this code index action and that will have a result named 'success'. That will reassign to index.gsp file. That's all for now. Thank you very much. See you later.