The int validator checks that given number is witin the specified range. It can be used in productId, employeeId etc.
Parameters of int validator
There is 3 parameters defined for int validator.
Parameter | Description |
fieldName | specifies the field name that is to be validated. It is required in Plain-Validator only. |
min | specifies the minimum value. It is ignored bydefault. |
max | specifies the maximum value. It is ignored bydefault. |
Example of int validator
- <validators>
-
- <validator type="int">
- <param name="fieldName">age</param>
- <param name="min">16</param>
- <param name="max">50</param>
- <message>Age must be between ${min} and ${max}</message>
- </validator>
-
- </validators>
- <validators>
-
- <field name="age">
- <field-validator type="int">
- <param name="min">16</param>
- <param name="max">50</param>
- <message>Age must be between ${min} and ${max}</message>
- </field-validator>
- </field>
-
- </validators>
Full example of int 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="id">
- <field-validator type="int">
- <param name="min">1</param>
- <param name="max">999</param>
-
- <message>Id must be between ${min} to ${max}</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"/>