Javatpoint Logo

91-9990449935

 0120-4256464

Struts 2 requiredstring validation example

The requiredstring validator specifies that string can't be null or blank.

It trims the string bydefault then checks if its length is greater that 0.

Parameters of requiredstring validator

There are two parameters defined for requiredstring validator.

ParameterDescription
fieldNamespecifies the field name that is to be validated. It is required in Plain-Validator only.
trimtrims the field values. It is bydefault true means it is enabled bydefault.

Example of requiredstring validator

<validators>
    <!-- Plain-Validator Syntax -->
    <validator type="requiredstring">
        <param name="fieldName">username</param>
        <param name="trim">true</param>
        <message>username is required</message>
    </validator>
    
</validators>
<validators>
    <!-- Field-Validator Syntax -->
    <field name="username">
    	  <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>username is required</message>
       </field-validator>
    </field>

</validators>

Full example of requiredstring 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
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<STYLE type="text/css">
.errorMessage{color:red;}
</STYLE>
</head>
<body>

<s:form action="register">
<s:textfield name="username" label="Username"></s:textfield>
<s:password name="userpass" label="Password"></s:password>
<s:submit value="register"></s:submit>
</s:form>

</body>
</html>

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

RegisterAction.java
package com.javatpoint;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport{
private String username,userpass;

public String getUsername() {
	return username;
}

public void setUsername(String username) {
	this.username = username;
}

public String getUserpass() {
	return userpass;
}

public void setUserpass(String userpass) {
	this.userpass = userpass;
}

public String execute(){
	return "success";
}

}


3) Create the validation file

Here, we are using bundled validators to perform the validation.

Register-validation.xml
<?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="username">
  		<field-validator type="requiredstring">
  		<message>Name can't be blank</message>
  		</field-validator>
  		</field>
  		
  		
  		</validators>

4) Create struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.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="register" class="com.javatpoint.Register">
<result name="input">index.jsp</result>
<result>welcome.jsp</result>
</action>

</package>
</struts>    
 

5) Create view component

It is the simple jsp file displaying the information of the user.

welcome.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>

Welcome,<s:property value="username"/>