Friday, 10 July 2015

Struts2 String Length Validation Example

The stringlength validator specifies that string must be of given length. It can be used in username, password etc.
It trims the string bydefault then checks if its length is of the given length.

Parameters of stringlength validator

There are 4 parameters defined for stringlength validator.
ParameterDescription
fieldNamespecifies the field name that is to be validated. It is required in Plain-Validator only.
minLengthspecifies the minimum length. It is ignored bydefault.
maxLengthspecifies the maximum length. It is ignored bydefault.
trimtrims the field values. It is bydefault true means it is enabled bydefault.

Example of stringlength validator

  1. <validators>  
  2.    <!-- Plain Validator Syntax -->  
  3.     <validator type="stringlength">  
  4.          <param name="fieldName">password</param>  
  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.          </validator>  
  10.       
  11. </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.javatpoint;  
  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.javatpoint.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"/>