Struts 2 RequiredString validator

This section discusses RequiredString validator of Struts 2 framework. RequiredStringValidator checks the String field is not-null and its length is > 0. (i.e. it isn't "").

Struts 2 RequiredString validator

Struts 2 RequiredString validator

     

This section discusses RequiredString validator of Struts 2 framework. RequiredStringValidator checks the String field is not-null and its  length is > 0. (i.e. it isn't ""). The "trim" parameter determines the String before performing the length check. If unspecified, the String will be trimmed.

If user does not enter anything in the input filed and submits the forms, required String validator will generate error message. Then the error message is displayed to user.

This example demonstrates how to use Struts 2 RequiredString validation with a simple data entry form where user is prompted to enter his/her user number.  Follow the following steps to develop the example application:

Step 1: Create the xml file and add the following xml snippet in the struts.xml file.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <!-- Rose India Struts Tutorials -->
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />

  <package name="roseindia" namespace="/roseindia" extends="struts-default">
  
  <!-- Add actions here -->

  <!-- string validation -->

  <action name="stringValidation" class="net.roseindia.StringVaLidationAction">
  <result name="input">/pages/stringInputForm.jsp</result>
  <result name="error">/pages/stringInputForm.jsp</result>
  <result>/pages/stringSuccess.jsp</result>
  </action>

  <!-- Add actions here -->
  </package>


  <!-- Add packages here -->

</struts>

Step 2 : Create the input form.

stringInputForm.jsp

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

<html>

<head>
<title>Input form</title>
 <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
  type="text/css"/>
  <s:head/>
</head>

<body>
  
  <s:form method="POST" action="stringValidation">
 </td>
  </tr>
  <s:textfield label="Enter User Number" name="username" maxlength="10" />
  <s:submit />
  </s:form>

</body>

</html>

Step 3 : Create the Action class.

StringVaLidationAction.java

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;

public class StringVaLidationAction extends ActionSupport{

  private String username;

  public String execute() throws Exception{
  if (getUsername() != null){
  return SUCCESS;
  }
  else{
  return ERROR;
  }
  }

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

Step 4 : Create the validators. 

The validation.xml format is either <ActionClassName>-validation.xml or <ActionClassName>-<ActionAliasName>-validation.xml.

RequiredString validator: RequiredStringValidator checks the String field is not-null and it has a length > 0. (i.e. it isn't ""). The "trim" parameter determines the String before performing the length check. If unspecified, the String will be trimmed. If any string inputted in the text field then you jumped into the stringSuccess.jsp page. Otherwise, it displays the given message in the xml file (User Name is required). The RequiredString validator takes the following parameters: 

  • fieldName - This is the field name that has to validate. Required if using Plain-Validator Syntax otherwise not required.
  • trim - This is the field name of value before validating (default is true).

StringVaLidationAction-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">
  <param name="trim">true</param>
  <message>User Name is required</message>
  </field-validator>
  </field>

</validators>

When any string that have to be inputted by you in the text field then you must have to jump into stringSuccess.jsp page and displays your inputted string with "Welcome to Vinod "message.

stringSuccess.jsp

<%@page language="java" %>
<html>

<head>
  <title>Correct entry</title>
</head>

<body>
<b>Welcome to </b><%=request.getParameter("username"%>!
</body>

</html>

Output:

When this application executes, you get the following:

When you fill the following string as 'Vinod Kumar'

Then you get: