Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
🙋Introduction
2.
Int Validator✅
2.1.
Parameters of Int Validator🤔
2.2.
Ways to use Int Validator ✍️
3.
Example:👀
3.1.
1. 📃Index.jsp
3.2.
2. 📃SubmitAction.java
3.3.
3. 📃SubmitValidation.xml
3.4.
4. 📃Struts.xml
3.5.
5. 📃HelloUser.jsp
4.
Frequently Asked Questions
4.1.
What are bundled validators?
4.2.
Is the fieldName parameter required when using field validator syntax?
4.3.
Can we create our validation logic?
4.4.
Can we validate input using regex?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Int Validator

🙋Introduction

Sometimes you would want input to be in some specific range. For the same reason, we validate an input by checking if it follows certain specifications. 

int validator

For this purpose, we have many built-in validators called Bundled Validators that Struts2 Validation Framework provides. Some of the built-in validators in the framework are email validator, date validator, int validator, and some more. Let us now see how an int validator works in Struts2.

Int Validator✅

The int validator in the bundled validator checks for an integer if it is in a given range. The parameters defined for the int validator are :

Parameters of Int Validator🤔

S.No.

PARAMETER

PURPOSE

1.

fieldName It holds the field value the validator has to validate. It is required when we use Plain Validator Syntax.

2.

min It holds the minimum value for the field the validator is validating. It is not mandatory to have this parameter set.

3.

max It holds the maximum value for the field the validator is validating. It is not mandatory to have this parameter set.

Because an int validator is a bundled validator, there are two ways we can use it:

Ways to use Int Validator ✍️

 

1. Plain Validator Syntax: For Action level validation, we use plain validator syntax. We can apply a single validator to multiple fields. For example:

 

<validators>  
 <!-- Plain Validator Syntax for int validation -->  
          <validator type="int">  
              <param name="fieldName"> Field Name </param>  
              <param name="min"> minimum integer value </param>  
              <param name="max"> maximum integer value </param>  
              <message> Message to be displayed! </message>   
          </validator>
</validators>

 

2. Field Validator Syntax: For Field level validation, we use field validator syntax. We can apply multiple validators to a single field. For example: 

 

<validators>  
    <!-- Field Validator Syntax for int validation -->  
          <field name="Field Name">  
              <field-validator type="int">  
                  <param name="min"> minimum integer value </param>  
                  <param name="max"> maximum integer value </param>  
                  <message> Message to be displayed! </message>  
              </field-validator>  
          </field>  
  
</validators> 

 

An example will clarify how we can use an int validator in our programs.

Example:👀

1. 📃Index.jsp

Index.jsp file is the file that creates the form to receive inputs. It gets the employee id and salary of an employee.

jsp image

 

<%@ taglib uri="/struts-tags" prefix="s" %>  
<html>  
<head>  
<STYLE type="text/css">  
.errorMessage{color:red;}  
</STYLE>  
</head>  
<body>  
  
<s:form action="Submit">  
<s:textfield name="e_id" label="Employee Id"></s:textfield>  
<s:textfield name="salary" label="Salary"></s:textfield>  
<s:submit value="Submit"></s:submit>  
</s:form>  
</body>  
</html>

2. 📃SubmitAction.java

SubmitAction.java is the action class that extends the ActionSupport class and overrides the execute method.

java image

 

package com.Coding Ninjas Studio; 

import com.opensymphony.xwork2.ActionSupport;   
public class SubmitAction extends ActionSupport { 
    private int e_id; 
    private int salary; 

    public int getE_Id() {   
        return e_id; 
    } 
    public void setE_Id(int e_id) {   
        this.e_id = e_id; 
    } 
    public double getSalary() {   
        return salary; 
    } 
    public void setSalary(int salary) {   
        this.salary = salary; 
    } 
    public String execute() {   
        return "success"; 
    } 
}

3. 📃SubmitValidation.xml

SubmitValidation.xml is the file where we will validate the integer input using the int validator.

xml image

 

<?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="salary">  
        <field-validator type="int">  
        <param name="min"> 10000 </param>  
        <param name="max"> 50000 </param>  
          
        <message> Make sure the salary is between ${min} and ${max} range. </message>  
        </field-validator>  
        </field>  
         
        </validators>

4. 📃Struts.xml

It defines the page where the user will be directed when the input is all correct.

xml image

 

<?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="Submit" class="com.Coding Ninjas Studio.SubmitAction">  
<result name="input">Index.jsp</result>  
<result>HelloUser.jsp</result>  
</action>  
  
</package>  
</struts>

5. 📃HelloUser.jsp

It is the jsp file that displays the information for the user.

jsp image

 

<%@ taglib uri="/struts-tags" prefix="s" %>  
  
Employee Id:<s:property value="e_id"/><br/>  
Salary:<s:property value="salary"/>

Frequently Asked Questions

What are bundled validators?

Bundled validators are built-in validators that the struts2 framework provides. Using these validators, we can put specific criteria for the inputs.

Is the fieldName parameter required when using field validator syntax?

No, we require the fieldName parameter only when using plain validator syntax.

Can we create our validation logic?

We can create our validation logic by implementing the Validateable interface in the action class.  

Can we validate input using regex?

Yes, we can validate input using regex. We can use the regex validator to validate input using regex.

Conclusion

This article was an insight into bundled validators in struts2. We saw the different bundled validators and discussed the int validator in detail. We also learned the two ways we can use the int validator to validate our input. If you want to learn more about struts2, check out this article. Also, ace your DSA game by practicing daily problems on Coding Ninjas Studio and also read interview experiences for different companies.  

Also, prepare your core subjects like DBMSSoftware engineering, and computer networks to ace your following interview. Also, check out some hot topics like machine learningdeep learningcomputer vision, and big data. You can always try our paid courses to upskill yourself if you want. 

Happy Learning!

Live masterclass