First of all we need to import the necessary libraries in the lib folder, we don't need to import all the libraries that come with Struts 2 zip package.
After that, we need to modify the application descriptor file (web.xml) introducing the Struts2 filter that is called on every action call:
You can see from the web.xml picture that between the application name and the welcome-file I have inserted the Struts 2 filter and his mapping.
The most important file that Struts 2 needs for doing his job is the struts.xml file. Differently from Struts 1, the new version of the framework wants this file in the src folder of the project. Remember that Struts 1 needed this file, called struts-config.xml, in the Web Content folder of the web project.
There are some differences from the previous and this version:
- There are not the forward tags, because now we only need to specify a result tag with the name and the type of the result. The result type could be and redirectAction, a tiles, a jsp page in which case we don't need to specify the type, a jasper page, and so on...;
- There are not the ActionForm because we insert the form properties directly on the actions, so those are seen from the jsp without the need to specify any ActionForm.
A new important feature of Struts 2 are the Interceptors. Almost everything in Struts 2 is made by the interceptors, and you can create your own specifying it in the struts.xml and creating the stack that manages it or inserting it in the default interceptor stack.
In the following picture you can see my struts.xml file and the action login:
This action has two results: the input result that is called when we have any error when inserting data on the form, that returns the same page. The success result that specifiies the loggedIn page as the next page we can see on the browser when the form has no errors.
In the next image, I'll show you the LoginAction that you saw declared in the struts.xml file. The Struts 2 Action classes extends the ActionSupport class and not the Action class as Struts 1 did. In the new version we must override the execute method that does not have any parameter and returns a String that is the name of the result that we specified in the configuration file.
As you can see from the image, I have only an instance variable that is of kind Account. I also need the getter and setter for this instance variable. In the execute method, the only thing that I do is looking if the values of account.userName and account.password matches "Alberto" and "Faedda" and, if the condition is true than I return the success result, else I add an action error and return the input result.
Another different thing that Struts 2 has from Struts 1 is how it uses the validation. In the previous version we had a unique validation.xml file in the Web Content folder, in which we specified every form of our application that we wanted to validate.
In this version we do have a different validation file for every form we want to validate, and we must create the file on the same package in which the action to which the form submits the parameters to, and his name will be <Action-name>-validation.xml.
It is much easyer to compile, because we don't have to specify the name of the forms on the file but only the fields we want to validate and the validation type for every field.
In the validation file of my login form, I only validate two fields: the username and password, and I look if those fields are not blank, else it shows an error in the jsp page, over the field in error without the need to add tags in the jsp page:
In the jsp page, I imported the struts 2 taglib, specifying as prefix for its use the s letter.
The form tag has as attributes the action name that will be called at the submission and the method type for the parameters tobe sent. We can also see the two textfields in which the name attribute is the name of the instance variables of the action class, in my case the Account instance variables userName and Password. After the submit tag, I have the actionerrors tag that specifies where in this page will be shown the error that occurs if the account login form submitted is wrong.
As you can see from the image, I don't have specified any other tag for the validation erorrs. It only needs to know the parameters name and the name of the action, specified in his ownfile name, and if any error occurs for the field, it automatically shows the error before the field.
The next image shows the index page. It is the page that we see when we start up the application to make the login.
Let's now see how we'll see the application in the web browser:
This is the index page in which we have our login form with the two textfields and the submit, and a register link under the form.
If we press the submit button and leave any textfield blank, as I told you before, we'll see the field errors that struts automatically set over the fields:
When we fill the form, and the field values on the fields doesn't match the values that I specified on the LoginAction class, it shows the actionerror under the form, where I set the tag:
If I insert the correct username and password I will log into the application...
...and it will show me the next page: