Struts2 ActionSupport addActionError

ActionSupport class implements many interfaces such as Action, Validateable and Serializable etc.

Here we will be using ActionSupport class to apply validation in action class.

Note: Before working on ActionSupport class to show field errors, please create Struts project using this Link.

Follow below steps once project setup is done and started working in local Eclipse.

  1. Open UserAction.java class and extends ActionSupport class. override the vaildate() method to handle validation errors and messages.

In this example we are going to use addActionError which will be responsible to an action error.

2. Update struts.xml file with below content to handle error and add below highlighted line red in color.

3. Update index.jsp with below content to take name and password as an input.

4. Update user.jsp to show name and password enter on index.jsp page

5. Run application and load below page:

http://localhost:8081/struts-demo

Do not enter name and password having length less than 4 will given below error

Provide name and password length greater than 4 will redirect to user.jsp page and print entered value on index.jsp page.

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.

0