Open In App

Struts 2 String Length Validation

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The string length field validator makes sure that a string field’s length, such as a password’s 6–12 characters, stays within a certain range. Using the stringlength property in elements or annotation types may be applied to XML or annotations. The String will have at least that many characters if the minLength option is supplied. When the maxLength argument is used, the String will only have that many characters at maximum. It decides whether to trim the String before doing the length check based on the trim option. The String will be truncated if it is not supplied.

Struts String Length Field Validator XML

<field name="fieldName">
<field-validator type="stringlength">
<param name="param name">param value</param>
<message>validation error message</message>
</field-validator>
</field>

Steps to Create Struts 2 String Length Validation

Step 1: Create an index.jsp file to receive user input

User input is received by the index.jsp page. After providing their name and password and pressing the “Register” button, the user will be redirected to the following resource.

HTML




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.geeksforgeeks.org/">
<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>


Step 2: Create welcome.jsp file to denote success

The page welcome.jsp appears. Upon executing the method and receiving the success string, welcome to the world of technology.

HTML




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.geeksforgeeks.org/">
  
  
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome page</title>
</head>
<body>
Welcome to the geeksforgeeks. 
</body>
</html>


Step 3: Create the action class LoginAction.java

There are two fields in the Action class LoginAction.java: name and password, along with getters and setters for each. It has the execute method in it.

Java




import com.opensymphony.xwork2.ActionSupport;
   
public class Login extends ActionSupport{
      
    private String userName;
    private String password;
   
    public String execute(){
        return SUCCESS;    
    }    
    //getters and setters
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}


Step 4: Created inside WEB-INF folder in WebContent folder

The processing of items is specified in the web.xml file. FilterDispatcher is entered into the web.xml file. The WebContent->WEB-INF folder is where this file was generated. The /* indicates that all urls will be processed. The struts filter completes this operation.

XML




<?xml version="1.0" encoding="UTF-8"?>
  <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>


Step 5: Create a Login-validation for Struts 2 String Length Validation

Add the action class LoginAction, along with a link to the result pages, to the struts.xml file. What browser is displayed following the action’s execution is determined by the outcome. Names for results that are optional include success, input, and error.

XML




<?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>


Step 6: Create the validation file

To validate the length of the password and the name, create the LoginAction-Validation.xml file.

XML




<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
    <head>
      <title>Struts 2 stringlength validator </title>
    </head>
    <body>
      <h3>This is the Struts 2 stringlength validator .</h3>        
      Hello <s:property value="userName" />     
    </body>
</html>


Output: Log in page:

Output in Browser

After entering username and password:

Username and Password entered



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads