Java Based Field validation

This tutorial will help us to throw and show error using Java based field validation. 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

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan