Struts Architecture Control Flow and Validation Framework
Struts Architecture Control Flow and Validation Framework
The Struts 2 provides supports to POJO based actions, Validation Support, AJAX
Support, Integration support to various frameworks such as Hibernate, Spring,
Tiles etc, support to various result types such as Freemarker, Velocity, JSP etc.
Struts 2 Features
Struts 2 provides many features that were not in struts 1. The important
features of struts 2 framework are as follows:
In struts 2, action class is POJO (Plain Old Java Object) i.e. a simple java class.
Here, you are not forced to implement any interface or inherit any class.
3) AJAX support
4) Integration Support
We can simply integrate the struts 2 application with hibernate, spring, tiles
etc. frameworks.
We can use JSP, freemarker, velocity etc. technologies as the result in struts 2.
Struts 2 provides various types of tags such as UI tags, Data tags, control tags
etc to ease the development of struts 2 application.
Struts 2 provides three types of theme support: xhtml, simple and css_xhtml.
The xhtml is default theme of struts 2. Themes and templates can be used for
common look and feel.
Model 1 and Model 2 (MVC) Architecture
Before developing the web applications, we need to have idea about design
models. There are two types of programming models (design models)
1. Model 1 Architecture
2. Model 2 (MVC) Architecture
Model 1 Architecture
Servlet and JSP are the main technologies to develop the web applications.
JSP overcomes almost all the problems of Servlet. It provides better separation
of concern, now presentation and business logic can be easily separated. You
don't need to redeploy the application if JSP page is modified. JSP provides
support to develop web application using JavaBean, custom tags and JSTL so
that we can put the business logic separate from our JSP that will be easier to
test and debug.
As you can see in the above figure, there is picture which show the flow of the model1
architecture.
Model The model represents the state (data) and business logic of the application.
View The view module is responsible to display data i.e. it represents the presentation.
Controller The controller module acts as an interface between view and model. It intercepts all
the requests i.e. receives input and commands to Model / View to change accordingly.
The directory structure of struts 2 is same as servlet/JSP. Here, struts.xml file must be located in
the classes folder.
2) Create input page (index.jsp)
This jsp page creates a form using struts UI tags. To use the struts UI tags, you
need to specify uri /struts-tags. Here, we have used s:form to create a form,
s:textfield to create a text field, s:submit to create a submit button.
index.jsp
web.xml
This is simple bean class. In struts 2, action is POJO (Plain Old Java Object).
It has one extra method execute i.e. invoked by struts framework by default.
Product.java
package com.swar;
return id;
this.id = id;
return name;
this.name = name;
return price;
return "success";
5) Map the request in (struts.xml) file and define the view components
It is the important file from where struts framework gets information about the
action and decides which result to be invoked. Here, we have used many
elements such as struts, package, action and result.
Result element is the sub element of action. It represents an view (result) that
will be invoked. Struts framework checks the string returned by the action
class, if it returns success, result page for the action is invoked whose name is
success or has no name. It has name and type attributes. Both are optional. If
you don't specify the result name, by default success is assumed as the result
name. If you don't specify the type attribute, by default dispatcher is considered
as the default result type.
struts.xml
It is the view component the displays information of the action. Here, we are
using struts tags to get the information.
The s:property tag returns the value for the given name, stored in the action
object.
welcome.jsp
To run this application, you need to have the struts 2 jar files. Here, we are
providing all the necessary jar files for struts 2. Download it and put these jar
files in the lib folder of your project.
Now copy following files from struts 2 lib folder C:\struts-2.2.3\lib to our project's WEB-INF\lib
folder. To do this, you can simply drag and drop all the following files into WEB-INF\lib folder.
commons-fileupload-x.y.z.jar
commons-io-x.y.z.jar
commons-lang-x.y.jar
commons-logging-x.y.z.jar
commons-logging-api-x.y.jar
freemarker-x.y.z.jar
javassist-.xy.z.GA
ognl-x.y.z.jar
struts2-core-x.y.z.jar
xwork-core.x.y.z.jar
8) start server and deploy the project
Finally, start the server and deploy the project and access it.
To create web project, click on the file menu - new - project - web project - write the project
name e.g. firststruts - finish.
2) Add struts 2 capabilities
To add struts 2 capabilities, select you project - click on the myeclipse menu - add project
capabilities - add struts capabilities.
index.jsp
It is the simple action class containing properties with setters and getters. It contains the execute
method also for defining the business logic.
Product.java
1. package com.javatpoint;
2.
3. public class Product {
4. private int id;
5. private String name;
6. private float price;
7. public int getId() {
8. return id;
9. }
10. public void setId(int id) {
11. this.id = id;
12. }
13. public String getName() {
14. return name;
15. }
16. public void setName(String name) {
17. this.name = name;
18. }
19. public float getPrice() {
20. return price;
21. }
22. public void setPrice(float price) {
23. this.price = price;
24. }
25.
26. public String execute(){
27. return "success";
28. }
29. }
5) Map the request in (struts.xml) file and define the view components
struts.xml
This jsp page displays the information set in the action object.
welcome.jsp
To start the server and deploy the project, right click on your project - Run As - MyEclipse
server application.
Here, we will take an example of an Employee whose name and age should be
captured using a simple page, and we will put these two validations to make sure
that the user always enters a name and age which should be in a range between 28
and 65.
Let us write main page JSP file index.jsp, which will be used to collect Employee related
information mentioned above.
<html>
<head>
<title>Employee Form</title>
</head>
<body>
<s:form action = "empinfo" method = "post">
<s:textfield name = "name" label = "Name" size = "20" />
<s:textfield name = "age" label = "Age" size = "20" />
<s:submit name = "submit" label = "Submit" align="center" />
</s:form>
</body>
</html>
The index.jsp makes use of Struts tag. just assume that the s:textfield tag prints a input field, and
the s:submit prints a submit button. We have used label property for each tag which creates label
for each tag.
Create Views
We will use JSP file success.jsp which will be invoked in case defined action returns SUCCESS.
<html>
<head>
<title>Success</title>
</head>
<body>
Employee Information is captured successfully.
</body>
</html>
Create Action
So let us define a small action class Employee, and then add a method called validate() as shown
below in Employee.java file. Make sure that your action class extends the ActionSupport class,
otherwise your validate method will not be executed.
package com.abc.struts2;
import com.opensymphony.xwork2.ActionSupport;
As shown in the above example, the validation method checks whether the 'Name' field has a value
or not. If no value has been supplied, we add a field error for the 'Name' field with a custom error
message. Secondly, we check if entered value for 'Age' field is in between 28 and 65 or not, if this
condition does not meet we add an error above the validated field.
Configuration Files
Finally, let us put everything together using the struts.xml configuration file as follows −
<struts>
<package name = "helloworld" extends = "struts-default">
</package>
</struts>
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Now do not enter any required information, just click on Submit button. You will see the
following result:
Enter the required information but enter a wrong From field, let us say name as "test" and age as
30, and finally click on Submit button. You will see the following result.
How this Validation Works?
When the user presses the submit button, Struts 2 will automatically execute the validate method
and if any of the “if” statements listed inside the method are true, Struts 2 will call its addFieldError
method. If any errors have been added, then Struts 2 will not proceed to call the execute method.
Rather the Struts 2 framework will return input as the result of calling the action.
Hence, when validation fails and Struts 2 returns input, the Struts 2 framework will redisplay the
index.jsp file. Since, we used Struts 2 form tags, Struts 2 will automatically add the error messages
just above the form filed.
These error messages are the ones we specified in the addFieldError method call. The
addFieldError method takes two arguments. The first, is the form field name to which the error
applies and the second, is the error message to display above that form field.
To handle the return value of input we need to add the following result to our action node in
struts.xml.
<result name="input">/index.jsp</result>