Javatpoint Logo
Javatpoint Logo

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.

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

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


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.

<%@ taglib uri="/struts-tags" prefix="s" %>
<STYLE type="text/css">

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


2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.
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.

<?xml version="1.0" encoding="UTF-8"?>

  <!DOCTYPE validators PUBLIC 
  		"-//OpenSymphony Group//XWork Validator 1.0.2//EN" 

  		<field name="username">
  		<field-validator type="requiredstring">
  		<message>Name can't be blank</message>

4) Create struts.xml

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "">

<package name="default" extends="struts-default">
<action name="register" class="com.javatpoint.Register">
<result name="input">index.jsp</result>


5) Create view component

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

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

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

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA