Struts2 String Length Validation Example


The stringlength validator indicates that string must be of given length. It tends to be utilized in username, secret word etc.

It trims the string bydefault then checks if its length is of the given length.

Parameters of stringlength validator

There are 4 parameters characterized for stringlength validator.

ParameterDescription
fieldNamespecifies the field name that will be approved. It is required in Plain-Validator only.
minLength indicates the base length. It is overlooked bydefault.
maxLength indicates the greatest length. It is disregarded bydefault.
trimtrims the field esteems. It is bydefault genuine methods it is empowered bydefault.

Example of stringlength validator

<validators>  
  •    <!-- Plain Validator Syntax -->  
  •     <validator type="stringlength">  
  •          <param name="fieldName">password</param>  
  •              <param name="minLength">6</param>  
  •              <param name="maxLength">10</param>  
  •              <param name="trim">true</param>  
  •              <message>Password must be between 6 to 10 characters long</message>          
  •          </validator>  
  •       
  • </validators>  
    1. <validators>  
    2.     <!-- Field-Validator Syntax -->  
    3.     <field name="password">  
    4.           <field-validator type="stringlength">  
    5.           <param name="minLength">6</param>  
    6.           <param name="maxLength">10</param>  
    7.           <param name="trim">true</param>  
    8.           <message>Password must be between 6 to 10 characters long</message>         
    9.        </field-validator>  
    10.     </field>  
    11.   
    12. </validators>  

    Full example of stringlength 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.   
    10. <s:form action="register">  
    11. <s:textfield name="username" label="Username"></s:textfield>  
    12. <s:password name="userpass" label="Password"></s:password>  
    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 .Net;  
    2.   
    3. import com.opensymphony.xwork2.ActionSupport;  
    4.   
    5. public class Register extends ActionSupport{  
    6. private String username,userpass;  
    7.   
    8. public String getUsername() {  
    9.     return username;  
    10. }  
    11.   
    12. public void setUsername(String username) {  
    13.     this.username = username;  
    14. }  
    15.   
    16. public String getUserpass() {  
    17.     return userpass;  
    18. }  
    19.   
    20. public void setUserpass(String userpass) {  
    21.     this.userpass = userpass;  
    22. }  
    23.   
    24. public String execute(){  
    25.     return "success";  
    26. }  
    27.   
    28. }  

    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="username">  
    10.         <field-validator type="requiredstring">  
    11.         <message>Name can't be blank</message>  
    12.         </field-validator>  
    13.         </field>  
    14.           
    15.         <field name="userpass">  
    16.         <field-validator type="requiredstring">  
    17.         <message>Password can't be blank</message>  
    18.         </field-validator>  
    19.         <field-validator type="stringlength">  
    20.         <param name="minLength">6</param>  
    21.         <param name="maxLength">10</param>  
    22.         <message>Password must be greater than or equal to 6 and less than or equal to 10</message>  
    23.         </field-validator>  
    24.           
    25.         </field>  
    26.           
    27.         </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.   
    5. <package name="default" extends="struts-default">  
    6. <action name="register" class="com.Javacodegeeks .Net.Register">  
    7. <result name="input">index.jsp</result>  
    8. <result>welcome.jsp</result>  
    9. </action>  
    10.   
    11. </package>  
    12.   
    13. </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.   
    3. Welcome,<s:property value="username"/>  





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