Web Application Development in Java training
Struts is a family of powerful MVC frameworks for Java.
Struts 1.x is the previous generation framework which is still being used in a lot of legacy applications.
Struts 2.x is a new framework (originally known as WebWork 2) which is now popular. As it is an MVC framework, it naturally supports Models, Views as well as Controllers.
In Struts 2.x, a request is processed by a Filter called FilterDispatcher. This filter is configured by an XML file called struts.xml (found in WEB-INF/classes) and which contain Actions.
When processing a request, the FilterDispatcher checks whether it matches one of the defined Actions. If true, the execute() method of the Action is called (this is where the Action can interact with Models) and that method then returns one of the various Struts 2.x constants (SUCCESS, INPUT, ERROR, etc.) An appropriate View is then shown depending on the return value. The View can easily access the properties of the Action.
In essence, Actions are like Controllers. Their execute method is called when a request comes. The Action interacts with Models and select a View.
Work to do
- Create a new Java dynamic web application.
- Add to it the following Struts libraries (commons-fileupload, commons-io, freemarker, javassist, ognl, struts2-core and xwork-core). The libraries can be found in the Struts 2 lib directory except for javassist which is the Blank demo application.
- Add a filter (FilterDispatcher) with URL pattern /* (i.e. everything!)
- Remove the welcome-file-list
- Create a HelloWorld JavaBean which implements Action and, consequently, the execute() method. Make sure it has (at least) a getter for a property called message (a String).
- Test it using unit testing.
- Create /WEB-INF/classes/struts.xml and reference the HelloWorld action in it. Make sure that the default view is HelloWorld.jsp
- Create HelloWorld.jsp and make sure you interact with the Action in it. The best way to do that is to use the Struts 2.x tag library. Here, <s:property> needs to be used.
- Test it!
Let’s now create a simple menu:
- Create a View called Welcome.jsp. Make sure that the view has a link to the HelloWorld action.
- Add a mapping without an action to struts.xml
- Test!
- Change the mapping to wildcard mapping.
- Test!
Adding a login form
Struts 2.x also supports form tags which allow complex forms to be built very quickly.
- Create a Login form with two fields: username and password. Do not use the classical HTML tags here. Use the Struts 2.x tags instead. The action of the form should be Authenticate (which has not been written yet!)
- Add this view as a mapping with a corresponding action in struts.xml
- Add Login to the menu to make it easier to access.
- Test!
- Add the Authenticate action. execute should only return SUCCESS when both the username and the password are “java” and, consequently, a nice “Authentication Done” needs to be shown. If one of the values has not been entered, the login form needs to be displayed again. Otherwise, “Authentication Error” should be displayed. How many new views do we need?
- Test!
Validation on the client
Struts 2.x has full support for server-side and client-side validation.
- Add a EnterDetails view as pictured above.
- Make sure that the GetCharacter as well as DisplayCharacter view work well.
Now, let’s make validation work by (1) adding validate=”true” to the form and (2) by adding something similar to the following to the GetCharacter action:
@Validations ( requiredFields = { @RequiredFieldValidator(fieldName="string", message="The string needs to be specified"), @RequiredFieldValidator(fieldName="position", message="The position needs to be specified") }, requiredStrings = { @RequiredStringValidator(fieldName="string", message="The string needs to contain characters") }, stringLengthFields = { @StringLengthFieldValidator(fieldName="string", minLength="1", maxLength="100", message="The string must be between 1 and 100 characters long") }, intRangeFields = { @IntRangeFieldValidator(fieldName="position", min="1", max="100", message="The position should lie between 1 and 100") } )
What do you notice? Where is validation being done?
Validation on the server
In some applications, it’s better (not to say simpler) to validate on the server instead of on the client. To do that, do the following simple changes:
- Remote validate=”true” in the JSP.
- Make the action extend ActionSupport instead of implementing Action
What do you observe when the application is run?
Further validation
ActionSupport has a method called validate() which can be used for more complex validations. For example, validate() can query databases and do calculations before emitting errors.
Web Application Development in Java training
Leave a Reply