Java Struts 2 Components

Java Struts 2 Components

In this article, I am going to discuss Java Struts 2 Components. Please read our previous article where we discussed Model 1 and Model 2 (MVC) Architecture. At the end of this article, you will understand the following pointers in detail which are related to Java Struts 2 Components.

  1. Action Beans
  2. Action servlets
  3. ActionForm beans and Custom Tags
  4. Interceptors
  5. Value Stack / OGNL
  6. Results / Result types
  7. View technologies
  8. Action context
  9. ActionInvocation

Let us understand the need and use of the above struts components one by one in detail.

What is the Bean tag?
  1. It is used to create an instance of a bean in a JSP page.
  2. It is a combination of the set and push tags.
  3. The bean should have a no-argument constructor.
What is/are the syntax of Action Beans?

In JSP form –
<s:bean name=”org.apache.struts2.example.counter.SimpleCounter” var=”counter”>
<s:param name=”foo” value=”BAR” />
The value of foot is : <s:property value=”foo”/>, when inside the bean tag <br />

In Freemarker form –
[@s.bean name=”org.apache.struts2.example.counter.SimpleCounter” var=”counter”]
[s:param name=”foo” value=”BAR”/]
The value of foo is : [s:property value=”foo”/], when inside the bean tag.


ActionServlet in Struts2 framework is used to develop the Model-View-Controller (MVC) based on web application which is basically designing for the pattern which is commonly called as “Model 2”. In this process, all the requests to the server move through the controller and the responsibility of the controller is to hand over all the requests. Struts Flow start and then call to process() method which is necessary to service the request from the user.

The tasks that are assigned to the ActionServlet are as follows:

  1. It identifies the substring that will be used to select an action process from the incoming request URI.
  2. The substring is used to map the Java class name of the corresponding dispatcher.
  3. For a particular dispatcher if a request is made the first time, then that class is instantiated and it will be cached for future usage.
  4. Otherwise, the properties of an ActionForm Bean are associated with the mapping.
  5. The execute method of the dispatcher is called while providing the access to the mapping that was used before, any relevant form-bean (if present), and the request as well as the response that was passed to the controller by the servlet container.
ActionForm Beans

ActionForm is a java bean that combines one or more than one ActionMappings. It maintains the session state for web applications. The ActionForm object is populated automatically on the server side when data is entered on a client-side.

What are Custom tags?

These are user-defined tags. They isolate business logic from JSP pages and also removes the possibility of scriptlet tags. Due to the use of custom tags the same business logic can be used.

Why do we use Custom tags?
  1. Eliminating the need for scriptlet tag the non-existence of any scriptlet tags gives a good coding approach.
  2. Separation of business logic from JSP isolates business logic from JSP pages, which makes the maintenance much easier.
  3. Re-usability Due to the use of custom tags the same business logic can be used repeatedly.
How to use Custom Tags?

1. <prefix:tagname attr1=value1….attrn=valuen />
2. <prefix:tagname attr1=value1….attrn=valuen >
            body code

What is an Interceptor?

At the time of preprocessing and postprocessing of a request, the interceptor object is called. Many features are implemented in Struts 2 using interceptors such as – validation, exception handling, file uploading, etc.

Why Interceptor is used?

To delete any concern like validation, exception handling, logging, etc. from the application, there is no need for the redeployment of the application, what we need to do is that just eliminate the entry from the struts.xml file.

What are the different kinds of default interceptors?
  1. alias – converts similar types of parameters that have disparate names between requests.
  2. chain – makes the properties of preceding action that are available in the current action and also used with chain result type.
  3. checkbox – used to handle the checkboxes in the sequence manner. And through this, we can easily identify the unchecked checkboxes.
  4. cookie – used to add a cookie to the current action.
  5. conversionError – used to add conversion errors to the action’s field errors.
  6. createSession – used to create the HttpSession object if it doesn’t exist.
  7. clearSession – releases the HttpSession object.
  8. debugging – used to provide support for debugging.
  9. execAndWait – used to send an intervening waiting page for the result.
  10. exception – used to guide an exception to a result.
  11. fileUpload – used to provide support to file upload in struts 2.
  12. i18n – used to provide support to internationalization and localization.
  13. jsonValidation – provides support to asynchronous support.
  14. logger – used to find out the action name.
  15. store – used to store and repossess action messages, action errors for action that implements the ValidationAware interface.
  16. modelDriven – used to make other model objects as the default object of valuestack.
  17. scopedModelDriven – same as ModelDriven but works only for action that implements ScopedModelDriven.
  18. params – colonizes the action properties with the request parameters.
  19. actionMappingParams
  20. prepare – used to perform preparation logic if action implements a Preparable interface.
  21. profiling – used to support action profiling.
  22. roles – used to support role-based action.
  23. scope – used to store the action state in the session or application scope.
  24. servletConfig – used to provide access to maps that represent HttpServletRequest and HttpServletResponse.
  25. staticParams – pointing the static properties to action properties.
  26. timer – gives the output to the time that needed to execute an action.
  27. token – prevents imitation submission of the request.
  28. tokenSession – prevents imitation submission of the request.
  29. validation – used to provide support to input validation.
  30. workflow – can call the validate method of action class if any action class implements Validateable interface.
How to configure Interceptors?

In the Struts.xml file –

<package name="default" extends="struts-default">
       <interceptor name="timer" class=".."/>
       <interceptor name="logger" class=".."/>
   <action name="login" class="tutorial.Login">
      <interceptor-ref name="timer"/>
      <interceptor-ref name="logger"/>
      <result name="input">login.jsp</result>
      <result name="success" type="redirectAction">/secure/home</result>
What is ValueStack?

A ValueStack is simply a stack that contains application-specific objects such as Action object, Model object, Named objects, and Temporary objects. Though the action is placed at the top of the stack during the execution time.

What are the methods that are used in ValueStack?

findValue(String expr): To find a value for a specific expression
Syntax: public Object findValue(String expr)

findString(String expr): To find a string for a specified expression
Syntax: public String findString(String expr)

peek(): It returns the object which is at the top of the stack without removing it
Syntax: public Object peek()

pop(): It returns the object which is at the top of the stack and removes it from the stack
Syntax: public Object pop()

push(Object o): Put the specified object on the top of the stack
Syntax: public void push(Object o)

set(String key, Object value): Sets an object on the stack with the specified key, which can be retrieved using findValue(key)
Syntax: public void set(String key, Object value)

size(): It gives the no. of objects in the stack
Syntax: public int size()

What is OGNL?

Object Graph Navigational Language (OGNL) is an open-source framework used to get properties from Java Beans. In Struts 2 –

  1. It is used to reference and manipulate data on the ValueStack.
  2. performs two important tasks – data transfer and type conversion.
What are the features of OGNL?
  1. type conversion
  2. calling methods
  3. collection manipulation and generation
  4. projection across collections
  5. expression evaluation
  6. lambda expressions
What is the result?

It acts as a view in the Struts2 MVC framework. It is responsible for redirecting to the destination page which can be specified between the <result> elements together with the name of the result (that is, type of result). The type of result is referred to by the result name.

Result Types

Predefined result types which are returned by the action class and compared here as the string data type.

  1. String SUCCESS = “success”
  2. String NONE = “none”
  3. String ERROR = “error”
  4. String INPUT = “input”
  5. String LOGIN = “login”
User- defined result types
  1. The dispatcher result – used to include or forward to a target view page (JSP).
  2. Chain action result – used to achieve action chaining. The multiple actions can be executed in a defined sequence and at the last action, the target view page will be displayed.
  3. Freemarker result – integrate the Freemarker templates on the view page.
  4. Redirect result – used to redirect the browser request to the new resource.
  5. Redirect action – same as redirect result type. But the target result must be an action either in the same application or in the other application.
  6. Stream result – used to Streaming the InputStream back to the client and the client can download the content in the specified format.
  1. It is basically a container that stores the objects that are related to the current request-response of a web application.
  2. It is created during a session, or application or locale, etc. during a complete client-server interaction.
  3. The objects are stored in a particular manner such as Temporary Objects -> Model Object -> Action Object -> Named Object.
  4. It consists of all the objects of ValueStack + other objects related to the current interaction of web applications to the client.

Java Struts 2 Components

What is ActionInvocation?

It represents the execution state of activity. It holds the action and interceptor’s objects.

What are the ActionInvocation methods?
Method Description
public ActionContext getInvocationContext() returns the ActionContext object related with the ActionInvocation.
public ActionProxy getProxy() returns the ActionProxy occasion holding this ActionInvocation.
public ValueStack getStack() returns the occasion of ValueStack.
public Action getAction() returns the occasion of Action related with this ActionInvocation.
public void invoke() invokes the following asset in handling this ActionInvocation.
public Result getResult() returns the occasion of Result.

In the next article, I am going to discuss Java Struts 2 Architecture and Flow in detail. Here, in this article, I try to explain Java Struts 2 Components and I hope you enjoy this Java Struts 2 Components article.

Leave a Reply

Your email address will not be published. Required fields are marked *