Introduction
Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time. Apache Struts 2 was originally known as WebWork 2.
The Struts 2 framework is used to develop MVC (Model View Controller) based web applications. Struts 2 is the combination of webwork framework of opensymphony and struts1.
struts2 = webwork + struts1
Architecture
Request Life Cycle
About Struts 2 Request Flow
- The controller receives the user request and determine which Struts 2 action to invoke.
- The framework creates an instance of this action and associate it with the newly created instance of the ActionInvocation.
- In Struts 2 the invocation of action should pass through a series of interceptors as defined in the application's XML file.
- The framework calls the ActionInvocations invoke() method to start the execution of the action.
- Each time the invoke() method is called, ActionInvocation consults its state and executes whichever interceptor comes next.
- ActionInvocation hands control over to the interceptor in the stack by calling the interceptors intercept() method.
- The intercept() method of the interceptor in turn calls the invoke() method of the ActionInvocation till all the interceptors are invoked, in the end the action itself will be called and the corresponding result will be returned back to the user.
- Some interceptor do work before the action is executed and some do work after the action is executed. It's not necessary that it should do something each time it is invoked.
- These interceptors are invoke both before and after the action.
- First all the interceptors are executed in the order they are defined in the stack.
- Then the action is invoked and the result is generated.
- Again all the interceptors present in the stack are invoked in the reverse order.
- The other important features of Struts 2 are OGNL and ValueStack.
- Object-Graph Navigation Language (OGNL) is a powerful expression language that is used to reference and manipulate data on the ValueStack.
- OGNL help in data transfer and type conversion.
- OGNL expression language provides simplified syntax to reference java objects.
- OGNL is used to bind the java-side data properties to the string-based view layer.
How Struts2 works
Struts 2 MVC is realised by three core framework components: actions, results, and the ServletFilter. The diagram below shows how these three components interact with each other.
1.1. Servlet Filter
The servlet filter acts as a controller in Struts 2. It inspects each incoming request to determine which Struts 2 action should handle the request. The framework handles all of the controller work for the application. All request URLs need to be mapped to actions with XML-based configuration files or Java annotations.
1.2. Interceptors
Interceptors execute before and after the request processing. They provide cross-cutting tasks so that they can be easily reused as well as separated from other architectural concerns. For example, validation parameters before invoking login action.
1.3. Action
Action handles the client requests in two different ways. First, the action plays an important role in the transfer of data from the request through to the view, whether its a JSP or other type of result. Second, the action must assist the framework in determining which result should render the view that will be returned in the response to the request.
Note-: In Struts 2 the model is implemented by the Action component.
1.4. Result
Result is a JSP or HTML page to create a view for client response. Struts 2 provides their own tags that we can use in JSP pages to create a response. Struts tags are great example of JSP Custom Tags.
Controller
StrutsPrepareAndExecuteFilter or FilterDispatcher(now depreciated) are used as front Controller in Struts2. In Struts 1, ActionServlet is front controller.
Example
web.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | < display-name >Struts2tutorial</ display-name > |
06 | < filter-name >Struts2</ filter-name > |
07 | < filter-class >org.apache.Struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</ filter-class > |
11 | < filter-name >Struts2</ filter-name > |
12 | < url-pattern >/*</ url-pattern > |
16 | < welcome-file >/login.jsp</ welcome-file > |
LoginAction.java
We only have one action class which implements Action
interface{This is optional in Struts 2.Read end of article for more detail}. The LoginAction
needs to implement the execute
method to handle client requests and return proper result.
package com.java.code.geeks.action; |
03 | import com.opensymphony.xwork2.Action; |
05 | public class LoginAction implements Action { |
08 | public String execute() throws Exception { |
09 | if (validateString(getUsername()) && validateString(getPassword())) |
15 | private String username; |
16 | private String password; |
18 | public String getUsername() { |
22 | public void setUsername(String username) { |
23 | this .username = username; |
26 | public String getPassword() { |
30 | public void setPassword(String password) { |
31 | this .password = password; |
34 | private boolean validateString(String str) { |
35 | if (str != null && !str.equals( "" )) |
login.jsp
01 | <%@ page language="java" contentType="text/html; charset=US-ASCII" |
02 | pageEncoding="US-ASCII"%> |
04 | <%@ taglib uri="/Struts-tags" prefix="s"%> |
07 | < meta http-equiv = "Content-Type" content = "text/html; charset=US-ASCII" > |
08 | < title >Login Page</ title > |
11 | < h3 >Welcome to Struts 2 Login Tutorial</ h3 > |
12 | < s:form action = "home" > |
13 | < s:textfield name = "username" label = "User Name" ></ s:textfield > |
14 | < s:textfield name = "password" label = "Password" type = "password" ></ s:textfield > |
15 | < s:submit value = "Login" ></ s:submit > |
welcome.jsp
01 | <%@ page language="java" contentType="text/html; charset=US-ASCII" |
02 | pageEncoding="US-ASCII"%> |
04 | <%@ taglib uri="/Struts-tags" prefix="s"%> |
07 | < meta http-equiv = "Content-Type" content = "text/html; charset=US-ASCII" > |
08 | < title >Welcome To Struts 2 Tutorial</ title > |
11 | < h3 >Welcome < s:property value = "username" ></ s:property >!</ h3 > |
error.jsp
01 | <%@ page language="java" contentType="text/html; charset=US-ASCII" |
02 | pageEncoding="US-ASCII"%> |
04 | <%@ taglib uri="/Struts-tags" prefix="s"%> |
07 | < meta http-equiv = "Content-Type" content = "text/html; charset=US-ASCII" > |
08 | < title >Error Page</ title > |
11 | < h4 >User Name or Password is wrong</ h4 > |
12 | < s:include value = "login.jsp" ></ s:include > |
Struts.xml
The Struts.xml
is created in resources folder in src. The Struts configs are responsible for mapping action
and result
in the web application.
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | <!DOCTYPE Struts PUBLIC |
04 | "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" |
07 | < package name = "default" namespace = "/" extends = "Struts-default" > |
09 | < result >/login.jsp</ result > |
11 | < action name = "home" class = "com.java.code.geeks.action.LoginAction" > |
12 | < result name = "SUCCESS" >/welcome.jsp</ result > |
13 | < result name = "ERROR" >/error.jsp</ result > |
For action login, there is no Action class and only one result. The client request will be forwarded to the login.jsp page when the login action is requested.
For action home, LoginAction is the action class and if execute() method returns SUCCESS the request will be processed by welcome.jsp and for ERROR it will be forwarded to error.jsp page. The namespace="/" is root namespace in Struts.
Now, we can access our application with URL http://localhost:8080/Struts2tutorial/login.action. Notice that URL is ending with .action that is the default suffix for Struts 2 action like it is .do for Struts 1.
Point to Remember-:
Struts 2 Action
In Struts 2 action class is a POJO. POJO means you are not forced to implement any interface or extend any class.In struts 2, action class is POJO (Plain Old Java Object). Generally, execute method should be specified that represents the business logic. The simple action class may look like:
Welcome.java
- package com.javatpoint;
- public class Welcome {
- public String execute(){
- return "success";
- }
- }
Action Interface
A convenient approach is to implement the com.opensymphony.xwork2.Action interface that defines 5 constants and one execute method.
5 Constants of Action Interface
Action interface provides 5 constants that can be returned form the action class. They are:
- SUCCESS indicates that action execution is successful and a success result should be shown to the user.
- ERROR indicates that action execution is failed and a error result should be shown to the user.
- LOGIN indicates that user is not logged-in and a login result should be shown to the user.
- INPUT indicates that validation is failed and a input result should be shown to the user again.
- NONE indicates that action execution is successful but no result should be shown to the user.
Let's see what values are assigned to these constants:
- public static final String SUCCESS = "success";
- public static final String ERROR = "error";
- public static final String LOGIN = "login";
- public static final String INPUT = "input";
- public static final String NONE = "none";
Method of Action Interface
Action interface contains only one method execute that should be implemented overridden by the action class even if you are not forced.
Example of Struts Action that implements Action interface
If we implement the Action interface, we can directly use the constants instead of values.
Welcome.java
- package com.javatpoint;
- import com.opensymphony.xwork2.Action;
- public class Welcome implements Action{
- public String execute(){
- return SUCCESS;
- }
- }
ActionSupport class
It is a convenient class that implements many interfaces such as Action, Validateable, ValidationAware, TextProvider, LocaleProvider and Serializable . So it is mostly used instead of Action.
Example of Struts Action that extends ActionSupport class
Let's see the example of Action class that extends the ActionSupport class.
Welcome.java
- package com.javatpoint;
- import com.opensymphony.xwork2.ActionSupport;
- public class Welcome extends ActionSupport{
- public String execute(){
- return SUCCESS;
- }
- }
Also Read-: ValueStack and OGNL differences.
No comments:
Post a Comment