Struts 2 double validation example


The double validator watches that given coasting point number is inside the predefined extend. It very well may be utilized in item cost etc.

Parameters of twofold validator

There is 5 parameters characterized for twofold validator.

ParameterDescription
fieldNamespecifies the field name that will be approved. It is required in Plain-Validator only.
minInclusivespecifies the base comprehensive esteem. It is overlooked bydefault.
maxInclusivespecifies the most extreme comprehensive esteem. It is overlooked bydefault.
minExclusivespecifies the base selective esteem. It is disregarded bydefault.
maxExclusivespecifies the most extreme select esteem. It is disregarded bydefault.

Example of twofold validator

Example of double validator

  1. <validators>  
  2.  <!-- Plain Validator Syntax -->  
  3.           <validator type="double">  
  4.               <param name="fieldName">price</param>  
  5.               <param name="minInclusive">122.0</param>  
  6.               <param name="maxInclusive">12200.0</param>  
  7.               <message>Price must be between ${minInclusive} and ${maxInclusive}</message>  
  8.           </validator>  
  9.             
  10. </validators>  
  1. <validators>  
  2.     <!-- Field Validator Syntax -->  
  3.           <field name="price">  
  4.               <field-validator type="double">  
  5.                  <param name="minInclusive">122.0</param>  
  6.                  <param name="maxInclusive">12200.0</param>  
  7.                  <message>Price must be between ${minInclusive} and ${maxInclusive}</message>  
  8.           </field-validator>  
  9.           </field>  
  10.   
  11. </validators>  

Full case of twofold validator

1) Create index.jsp for information

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

index.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <html>  
  3. <head>  
  4. <STYLE type="text/css">  
  5. .errorMessage{color:red;}  
  6. </STYLE>  
  7. </head>  
  8. <body>  
  9.   
  10. <s:form action="register">  
  11. <s:textfield name="id" label="Product Id"></s:textfield>  
  12. <s:textfield name="price" label="Product Price"></s:textfield>  
  13. <s:submit value="register"></s:submit>  
  14. </s:form>  
  15.   
  16. </body>  
  17. </html>  

2) Create the action class

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

RegisterAction.java
  1. package com.javacodegeeks;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class Register extends ActionSupport{  
  6. private int id;  
  7. private double price;  
  8.   
  9. public int getId() {  
  10.     return id;  
  11. }  
  12.   
  13. public void setId(int id) {  
  14.     this.id = id;  
  15. }  
  16.   
  17. public double getPrice() {  
  18.     return price;  
  19. }  
  20.   
  21. public void setPrice(double price) {  
  22.     this.price = price;  
  23. }  
  24.   
  25. public String execute(){  
  26.     return "success";  
  27. }  
  28.   
  29. }  

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="price">  
  10.         <field-validator type="double">  
  11.           <param name="minInclusive">122.0</param>  
  12.           <param name="maxExclusive">555.9</param>  
  13.           
  14.         <message>Price must be between ${minInclusive} to ${maxExclusive}</message>  
  15.         </field-validator>  
  16.         </field>  
  17.         </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="price"> <field-validator type="double"> <param name="minInclusive">122.0</param> <param name="maxExclusive">555.9</param> <message>Price must be between ${minInclusive} to ${maxExclusive}</message> </field-validator> </field> </validators>

4) Create struts.xml

This xml file defines an extra result 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 Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4. <package name="default" extends="struts-default">  
  5. <action name="register" class="com.javacodegeeks.Register">  
  6. <result name="input">index.jsp</result>  
  7. <result>welcome.jsp</result>  
  8. </action>  
  9.   
  10. </package>  
  11. </struts>      
  12.     
  13.    

5) Create view component

It is the simple jsp file displaying the information of the user.

welcome.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. Product Id:<s:property value="id"/><br/>  
  4. Product price:<s:property value="price"/>  





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