Struts 2 Stringlength Validator

In the struts web application, stringlength validator is used to check whether the length of the string is between the minimum and maximum length. Four parameters defined with stringlength validator are:

  • fieldName: It is used to specify the name of the field which is to be validated.
  • minLength: This parameter is used to describe the minimum string length allowed.
  • maxLength: This parameter is used to describe the maximum string length allowed.
  • trim: It is used to trim field values.

Struts Bundled Validators - stringlength Example:

To get input from the user create the index.jsp file:

The index.jsp page takes the input from the user. The user has to enter the name and password and then after clicking on the Register button, it will redirect to the next resource. It contains two textfield and one button on the form with the link to LoginAction class

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>  
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<s:form action="login">
<s:textfield name="name" label="Enter the name"></s:textfield>
<s:textfield name="pwd" label="Enter the password"></s:textfield>
<s:submit value="Register"></s:submit>
</s:form>
</body>
</html>

Create welcome.jsp file to denote success:

The welcome.jsp page displays Welcome to the world of the Technology when execute method return success string.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome page</title>
</head>
<body>
Welcome to the world of the Technology. 
</body>
</html>

Create error.jsp file to denote failure:

The error.jsp page displays Sorry wrong Username or Password !!! on failure. When the execute() method returns an error, this page executes.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error page</title>
</head>
<body>
Sorry wrong Username or Password !!!
</body>
</html>

Create the action class LoginAction.java:

The Action class LoginAction.java contains two fields name and password with their getter and setters. It contains the execute method. On success, it will go to the welcome.jsp page otherwise on error it will redirect to error.jsp.

import java.util.Date;


import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport
{
private String name;
private String pwd;


public String getName() 
{
return name;
}
public void setName(String name)
{
this.name = name;
}


public String getPwd() 
{
return pwd;
}
public void setPwd(String pwd) 
{
this.pwd = pwd;
}


@Override
public String execute() throws Exception 
{
return "success";
}


}

web.xml file is created inside WEB-INF folder in WebContent folder:

The web.xml file defines how elements are processed. The entry of FilterDispatcher is done in the web.xml file. This file is created in WebContent->WEB-INF folder. /* specifies all urls will be parsed. This task is done by struts filter.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsBundledValidation</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>  
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
   </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Construct struts.xml file:

In the struts.xml file, make the entry of the action class LoginAction and link for it and result pages. The result determines what browser will display after the execution of the action. Results have optional names like success, input and error.

<?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="login" class="LoginAction">  
<result name="success">welcome.jsp</result>  
<result name="error">error.jsp</result>
<result name="input">index.jsp</result>
</action>  
  
</package>  
</struts>

Create the validation file:

Create the LoginAction-Validation.xml file to validate name and password length.

<?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="name">  
        <field-validator type="requiredstring">  
        <message>Name can't be blank</message>  
        </field-validator>  
        </field>  
        
          
        <field name="pwd">  
        <field-validator type="requiredstring">  
        <message>Password can't be blank</message>  
        </field-validator>  
        <field-validator type="stringlength">  
        <param name="minLength">6</param>  
        <param name="maxLength">10</param>  
        <message>Password must be greater than or equal to 6 and less than or equal to 10</message>  
        </field-validator> 
        </field>
          
                 
        </validators>

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. It will show two labels for entering the name and password and the Register button.

Struts Bundled Validators - stringlength

Enter any name and enter the password whose length is less than 6.

Struts Bundled Validators - stringlength

Length of the password is less than 6, the error message will display.

Struts Bundled Validators - stringlength

Again run the application. This time enter any name and enter a password whose length is greater than 10.

Struts Bundled Validators - stringlength

As the length of the password is greater than 10, the error will display.

Struts Bundled Validators - stringlength

Again run the application. This time enter any name and enter a password whose length is greater than 6 and less than 10.

Struts Bundled Validators - stringlength

As the name and password are entered correctly, execute method will return a success string. It will redirect to page welcome.jsp and display a Welcome message.

Struts Bundled Validators - stringlength