Struts2 Complete Guide
Apache Struct 2 is an open source framework to build Java Enterprise Edition web applications. It is based on Model View Controller (MVC) design pattern. Action, Interceptors, ValueStack, OGNL are important features of Struts 2.
Struts 2 is the combination of webwork framework of opensymphony and struts 1.
Model View Controller
It is a very well known design pattern to develop web application.
Model → This is responsible for maintaining data. e.g. bean or model class.
View → It is a representation of the end result to output or show on the on the page. e.g. JSP, HTML etc.
Controller → Controller is a backend code which gets trigger on performing any action or user input.
MVC Architecture
The browser will call the controller as shown in the below diagram. The controller will decide whether to call view or model.
1st Scenario → The browser will call the controller and, depending on input data, the model will call the database as it is tightly coupled using JPA or Hibernate. On getting a response back from the database, the model will call view to output data on the browser output screen.
2nd Scenario → The browser will call the controller and, depending on the input data, the view will call the database as it is tightly coupled using JPA or Hibernate. On getting a response back from the database, the view will output data on the browser output screen.
Mode can get request to return data from both View and Controller.
Below are the mail features of Struts2:
Action
Action is an important feature of Struts2 framework. Action is an interface belongs to com.opensymphony.xwork2 package having static final variables and execute method.
Example:
Below is the example to implement Action interface:
Interceptor
Interceptor are like filters which get processed just before and after action called.
debugging, logging, i18n, params, validation are the default interceptors provided by Struts.
ValueStack
It is all about action and model objects. Action is placed at the top at the time of execution. Save, search and delete operations can be performed in valueStack.
findString, findValue, peek, pop, push and set are some of the ValueStack methods.
Syntax:
Note: ValueStack is collection of multiple Maps called as ApplicationContext.
ActionContext
ActionContext class is child of an Object class and implements Serializable interface .Action gets executed in ActionContext container. The value stored in ActionContext are unique per thread.
In simple, ActionContext is collection of Objects.
get, getName, getContext, setContainer, getContainer, put, setSession, setLocale, setParameters, setValueStack are some common methods of ActionContext class.
Syntax:
Note: Application context is a Map<String, Object>() and ValueStack is collection of multiple Maps (or ApplicationContext).
OGNL (Object Graph Navigation Language)
It is a JSP expression language used to access ValueStack data on front end. The properties of the object can be referenced using the pound symbol in JSP.
Example:
Line 1 is to access OGNL property directly.
Line2 and 3 is to access name property inside scope(e.g. request, session, etc.) object.
Struts Sample Code Example
Below is the simple and over all example of Struts2 overall code flow:
Step 1 → Execution flow will start from Form.jsp where user will submit an action as student and name input field with some value filled.
Step 2 → As soon as request reach to struts.xml and look for student action mapping class com.student.StudentAction.
Step 3 → process execute() method in mapped class com.student.StudentAction and return true or false to struts.xml.
Step 4 → success and error will execute <result> tag in struts.xml and redirect to either Student.jsp or Error.jsp
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.