Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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

                         

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:

                         

» View all related tutorials
Related Tags: c validation struts io release id ie with e it li no as m tr valid bus ase rel s

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.