Struts2 actionerror & actionmessage

 

In this example, we will see how to use actionerror tag to display an error message and actionmessage to display an informative message.

 

1. Technologies used

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

 

2. Project structure

Struts2ActionErrorMessage

 

3. Action class

We will override the validate() method to add some validation rules on name attribute.

File : HelloAction

 

4. JSP pages

File : index.jsp

 

We have used <s:actionerror /> tag to display the error message that we have set in the action class.

 

File : hello.jsp

 

And we have used <s:actionmessage /> tag to display the hello message that we have set in the action class.

 

5. Struts config

File : struts.xml

 

6. Test It

URL : http://localhost:8080/Struts2ActionErrorMessage/Hello/index.action

We first test the actionerror, we submit without entering value in the name field :

Struts2ActionErrorMessage1

 

Then we test the actionmessage, we enter a name, then we press the submit button, so we will see a hello message

Struts2ActionErrorMessage2

 

Download source code