Struts 2 Actions Example Tutorial

If you are working on Struts 2, you will spent a lot of time developing Struts 2 Actions. This article is aimed to provide you more details about Struts 2 Action classes and different ways to implement them.

Struts 2 Action Overview

Struts 2 Action is at the front of our application that takes care of handling client requests. You will notice that in any struts 2 application, there is an action class associated with different type of client action.

Struts 2 Action class does following tasks:

  1. Business Logic: Our application business logic resides in action classes. If your application has a lot of business logic, you might have other helper classes to do the actual work but it’s always Action class that integrates them with the application.
  2. Data Carrier: If you look into any Servlet code, you will notice that it’s our responsibility to get the client request data and process them. Most of the times we get the request data to create a java bean object and then invoke our business logic classes. Struts 2 Action classes makes our life easier with handling the data mapping to action class bean properties or to another java bean internally. It saves a lot of code that we used to write with Servlets. Similarly Struts Action takes care of the process to make sure result pages have access to the data that can be used in generating client response.Actually this task is done by Struts 2 params interceptor defined in struts-default package as:<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
  3. Determining Result: Struts 2 Action classes are responsible for determining the result pages that will be used for generating client response. It can be simple HTML page, JSP page etc.

There are four ways through which we can create Struts 2 Action classes.

  1. Simple Action Class: We can use any normal java class as Struts 2 action class, the only requirement is that it should have execute() method returning String. In the simplest form, we can have action class as:

    Now we can configure this in struts.xml file as:

    Now we can access this action with URL https://localhost:8080/Struts2ActionExample/home.action and it will return home.jsp page as response.

    Struts 2 default location for result pages are WEB-INF/content and we are overriding it with struts.convention.result.path configuration, read more about this at Struts 2 ResultPath.

  2. Implementing Action interface: We can implement com.opensymphony.xwork2.Action interface also to create action classes. Action interface has a single method execute() that we need to implement. The only benefit of using Action interface is that it contains some constants that we can use for result pages, these constants are SUCCESS, ERROR, NONE, INPUT and LOGIN. We can rewrite above HomeAction class by implementing Action interface as shown below.

    Read more about this approach at Struts 2 Beginners Tutorial.

  3. Using Struts 2 Annotations: Struts 2 supports annotation based configuration and we can use it to create action classes. To use struts 2 annotations, we need to add struts2-convention-plugin library in the classpath and in web.xml struts2 filter configuration provide the java package that contains action classes.

    We can rewrite HomeAction class using annotation as below.

    Read more about Struts 2 annotations at Struts 2 Annotations Hello World Example.

  4. Extending ActionSupport Class: ActionSupport class is the default implementation of Action interface and it also implements interfaces related to Validation and i18n support. ActionSupport class implements Action, Validateable, ValidationAware, TextProvider and LocaleProvider interfaces.We can override validate() method of ActionSupport class to include field level validation login in our action classes. For example, if we have a JSP page login.jsp as below.login.jsp

    Then we can have LoginAction class with form fields validation as:

    We can configure this action class as:

    Now if either of the fields name or pwd will be empty in request, addFieldError() method will add an error message and struts 2 will return the response with result name “input”. You will get the login.jsp as response with error messages mentioned in the page.

Depending on your requirements, you can use any of the approaches to create struts 2 action classes, my favorite is ActionSupport class because it helps in writing validation and i18n logic easily in action classes.

By admin

Leave a Reply

%d bloggers like this: