Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

int


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

  1. <validators>
  2.  <!– Plain Validator Syntax –>
  3.           <validator type=“int”>
  4.               <param name=“fieldName”>age</param>
  5.               <param name=“min”>16</param>
  6.               <param name=“max”>50</param>
  7.               <message>Age must be between ${min} and ${max}</message>
  8.           </validator>
  9. </validators>
  1. <validators>
  2.     <!– Field Validator Syntax –>
  3.           <field name=“age”>
  4.               <field-validator type=“int”>
  5.                   <param name=“min”>16</param>
  6.                   <param name=“max”>50</param>
  7.                   <message>Age must be between ${min} and ${max}</message>
  8.               </field-validator>
  9.           </field>
  10. </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

  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. <s:form action=“register”>
  10. <s:textfield name=“id” label=“Product Id”></s:textfield>
  11. <s:textfield name=“price” label=“Product Price”></s:textfield>
  12. <s:submit value=“register”></s:submit>
  13. </s:form>
  14. </body>
  15. </html>

2) Create the action class

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

RegisterAction.java

  1. package com.Kreationnext;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class Register extends ActionSupport{
  4. private int id;
  5. private double price;
  6. public int getId() {
  7.     return id;
  8. }
  9. public void setId(int id) {
  10.     this.id = id;
  11. }
  12. public double getPrice() {
  13.     return price;
  14. }
  15. public void setPrice(double price) {
  16.     this.price = price;
  17. }
  18. public String execute(){
  19.     return “success”;
  20. }
  21. }

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.   <!DOCTYPE validators PUBLIC
  3.         “-//OpenSymphony Group//XWork Validator 1.0.2//EN”
  4.         “http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd”>
  5.         <validators>
  6.         <field name=“id”>
  7.         <field-validator type=“int”>
  8.         <param name=“min”>1</param>
  9.         <param name=“max”>999</param>
  10.         <message>Id must be between ${min} to ${max}</message>
  11.         </field-validator>
  12.         </field>
  13.         </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.Kreationnext.Register”>
  6. <result name=“input”>index.jsp</result>
  7. <result>welcome.jsp</result>
  8. </action>
  9. </package>
  10. </struts>

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. Product Id:<s:property value=“id”/><br/>
  3. Product price:<s:property value=“price”/>

Comments

comments