Struts 2 Ajax Validation - jsonValidation Interceptor

struts 2 offers help to ajax validation. In such case, page won't be refreshed or reloaded so it will make the execution quick. It is verifiably done utilizing javascript for example utilized for the client side validation.

jsonValidation Interceptor

The AJAX validation is performed by jsonValidation interceptor. It isn't found in the default stack so we have to characterize it unequivocally. It doesn't play out any validation itself that is the reason it must be utilized with validation interceptor. It is found in the jsonValidationWorkflowStack, that incorporates jsonValidation, validation and work process interceptors and basicstack.


Steps to perform AJAX Validation

The basic strides to perform AJAX Validation are as follows:

  1. create the form to get input from the user
  2. Inherit the ActionSupport class in your action
  3. Define the validation in validation.xml file
  4. Define result name input for the error message and register the jsonValidationWorkflowStack in struts.xml document

Example to perform ajax validation

In this precedent, we are making 4 pages :

  1. index.jsp for input from the user.
  2. Register.java for business logic.
  3. Register-validation.xml for utilizing bundled validators.
  4. struts.xml for characterizing the interceptors and results for the action.
  5. welcome.jsp for the view component.

1) Create index.jsp for information

This jsp page makes a form utilizing struts UI labels. It gets name, receives phrase and email id from the client.

index.jsp
  1. <%@ taglib  uri="/struts-tags" prefix="s"%>  
  2. <%@ taglib uri="/struts-dojo-tags" prefix="d"%>  
  3. <html>  
  4. <head>  
  5. <d:head/>  
  6. </head>  
  7. <body>  
  8. <marquee>Registration Form.............</marquee>  
  9.   
  10. <s:form action="register">  
  11. <s:textfield name="name" label="Username"></s:textfield>  
  12. <s:textfield name="email" label="Email ID"></s:textfield>  
  13. <s:password name="password" label="Password"></s:password>  
  14. <d:submit validate="true" type="image" src="register-now.jpg">  
  15. </d:submit>  
  16. </s:form>  
  17.   
  18. </body>  
  19. </html>  

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

RegisterAction.java
  1. package mypack;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class Register extends ActionSupport{  
  6. private String name,password,email;  
  7.   
  8. //setters and getters  
  9.   
  10. public String execute(){  
  11.     return "success";  
  12. }  
  13. }  

3) Create the validation file

Here, we are using bundled validators to perform the validation.

Register-validation.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     
  3. <!DOCTYPE validators PUBLIC   
  4.   "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
  5.   "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  6.   
  7.     <validators>  
  8.           
  9.     <field name="name">  
  10.     <field-validator type="requiredstring">  
  11.     <message>Name can't be blank</message>  
  12.     </field-validator>  
  13.     </field>  
  14.           
  15.     <field name="email">  
  16.     <field-validator type="requiredstring">  
  17.     <message>Email ID can't be blank</message>  
  18.     </field-validator>  
  19.     <field-validator type="email">  
  20.     <message>Please enter a valid email ID</message>  
  21.     </field-validator>  
  22.     </field>  
  23.           
  24.     <field name="password">  
  25.     <field-validator type="requiredstring">  
  26.     <message>Password can't be blank</message>  
  27.     </field-validator>  
  28.     <field-validator type="stringlength">  
  29.     <param name="minLength">5</param>  
  30.     <param name="maxLength">10</param>  
  31.     <message>Password can't be less than 5 or greater than 10</message>  
  32.     </field-validator>  
  33.           
  34.     </field>  
  35.           
  36.     
  37.           
  38.     </validators>   <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <message>Name can't be blank</message> </field-validator> </field> <field name="email"> <field-validator type="requiredstring"> <message>Email ID can't be blank</message> </field-validator> <field-validator type="email"> <message>Please enter a valid email ID</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>Password can't be blank</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">5</param> <param name="maxLength">10</param> <message>Password can't be less than 5 or greater than 10</message> </field-validator> </field> </validators>

4) Create struts.xml

This xml document characterizes an additional outcome by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "- //Apache Software Foundation//DTD Struts  
  3.  Configuration 2.1//EN" "http://struts.apache.org/dtds/struts 2.1.dtd">  
  4. <struts>  
  5.   
  6. <package name="a" extends="struts-default">  
  7.   
  8. <action name="register" class="mypack.Register">  
  9. <interceptor-ref name="jsonValidationWorkflowStack"></interceptor-ref>  
  10.   
  11. <result name="success">/welcome.jsp</result>  
  12. <result name="input">/index.jsp</result>  
  13. </action>  
  14.   
  15. </package>  
  16. </struts>      

5) Create see component

It is the straightforward jsp document showing the data of the user.

welcome.jsp
  1. <%@ taglib uri="/struts tags" prefix="s" %>  
  2.   
  3. Welcome, <s:property value="name"/>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition