The double validator checks that given floating-point number is within the specified range. It can be used in product price etc.
Parameters of double validator
There is 5 parameters defined for double validator.
Parameter | Description |
fieldName | specifies the field name that is to be validated. It is required in Plain-Validator only. |
minInclusive | specifies the minimum inclusive value. It is ignored bydefault. |
maxInclusive | specifies the maximum inclusive value. It is ignored bydefault. |
minExclusive | specifies the minimum exclusive value. It is ignored bydefault. |
maxExclusive | specifies the maximum exclusive value. It is ignored bydefault. |
Example of double validator
- <validators>
-
- <validator type="double">
- <param name="fieldName">price</param>
- <param name="minInclusive">100.0</param>
- <param name="maxInclusive">10000.0</param>
- <message>Price must be between ${minInclusive} and ${maxInclusive}</message>
- </validator>
-
- </validators>
- <validators>
-
- <field name="price">
- <field-validator type="double">
- <param name="minInclusive">100.0</param>
- <param name="maxInclusive">10000.0</param>
- <message>Price must be between ${minInclusive} and ${maxInclusive}</message>
- </field-validator>
- </field>
-
- </validators>
Full example of double validator
1) Create index.jsp for input
This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.
index.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
- <html>
- <head>
- <STYLE type="text/css">
- .errorMessage{color:red;}
- </STYLE>
- </head>
- <body>
-
- <s:form action="register">
- <s:textfield name="id" label="Product Id"></s:textfield>
- <s:textfield name="price" label="Product Price"></s:textfield>
- <s:submit value="register"></s:submit>
- </s:form>
-
- </body>
- </html>
2) Create the action class
This action class inherits the ActionSupport class and overrides the execute method.
RegisterAction.java
- package com.javatpoint;
-
- import com.opensymphony.xwork2.ActionSupport;
-
- public class Register extends ActionSupport{
- private int id;
- private double price;
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- public String execute(){
- return "success";
- }
-
- }
3) Create the validation file
Here, we are using bundled validators to perform the validation.
Register-validation.xml
- <?xml version="1.0" encoding="UTF-8"?>
-
- <!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">100.0</param>
- <param name="maxExclusive">9999.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
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="default" extends="struts-default">
- <action name="register" class="com.javatpoint.Register">
- <result name="input">index.jsp</result>
- <result>welcome.jsp</result>
- </action>
-
- </package>
- </struts>
-
-
5) Create view component
It is the simple jsp file displaying the information of the user.
welcome.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
-
- Product Id:<s:property value="id"/><br/>
- Product price:<s:property value="price"/>