Struts2 ActionSupport Class

 

In this example, we will create a simple Struts2 application with an action that extends the ActionSupport class.

ActionSupport is a convenient class that provides a default implementation of many useful interfaces such as Action :

So, when an action class extends the ActionSupport, it can easily use some interesting features like validation and internationalization.

 

1. Technologies used

  • Struts 2.3.16
  • Maven 3.0.5
  • Eclipse 4.2
  • JDK 1.6
  • Tomcat 6.0

 

2. Project structure

Struts2-ActionSupport-Class-1

 

3. Struts2 dependency

File : pom.xml

 

4. JSP pages

File : index.jsp

 

File : hello.jsp

 

5. Action

File : HelloAction

 

Because we have extended the ActionSupport :

  • we implemented the execute() method.
  • we used directly the SUCCESS constant.
  • we used validate() method to add some validation logic.
  • we used addFieldError(..) method to add an error message for the name field.
  • we used getText(..) method to get message from the resource bundle.

 

6. Struts config

File : struts.xml

 

7. Test It

URL : http://localhost:8080/Struts2-ActionSupport-Class/Hello/index.action

Struts2-ActionSupport-Class-2

 

We press the button without entering a value :

Struts2-ActionSupport-Class-3

 

We enter some value and click submit button :

Struts2-ActionSupport-Class-4

 

Download source code