Struts XML based validation

In this tutorial we will be having a discussion over XML struts based validation.

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

Follow below rules to create validation using XML file:

  1. Create validation XML file with the same name of action as show below:

<action_class_name>-validation.xml 

2. The validation file needs to be place in the same folder where action class lies as shown below. 

<validators> root tag is to accommodate all field level validation tags.

field tag is to apply validation on specific field. e.g. <field name=”age”>

<field-validator type=”int”> or <field-validator type=”required”> to define field validation type.

<param name=”min”> is to define a condition to add for validation.

<message> is to declare error message to show end user.

Note: Same thing to validate name and password String can be achieve using UserAction.Java class extending ActionSupport class and override validate() method as shown in below code:

To check complete implementation, please click on link

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