Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

The example code for this tutorial, Formform_XML_Validation_Struts2_Ant or Form_XML_Validation_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/listImage Removed. After downloading and unzipping the file, you'll have a folder named Form_XML_Validation_Struts2_Ant (or Form_XML_Validation_Struts2_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example application.xml_validation, is available for checkout at https://github.com/apache/struts-examples

Introduction

In this tutorial we'll cover how to validate a user's input in form fields using Struts 2's XML validation methodology. In the Form Validation tutorial we discussed validating a user's input using the validate method in the Action class. Using a separate XML validation file gives you the ability to use validators built-in to the Struts 2 framework.

...

The example application that supports this tutorial (Form_XML_Validation_Struts2 available at http://code.google.com/p/struts2-examples/downloads/listImage Removed.) shows how to use Struts 2's XML validation methodology. The information that can be edited is encapsulated in an object of class Person.

To enable the user to edit his information that is stored in the Person object, we have this form:

Image RemovedImage Added

When the user submits the form, we want to validate his entries into the form fields.

...

To validate a user's form field entries you can use a separate XML file that contains your validation rules. The XML file that contains the validation rules must be named as ActionClassName-validation.xml. In the example application, the XML validation file is named EditAction-validation.xml (see src/main/resources/org/apache/struts/edit/action for the Maven example or src/org/apache/struts/edit/action for the Ant example).

Struts 2 provides several different validators that you can use in the XML validation file. See Validation for a list of validators you can employ.

...

In the XML validation file (for this example that is EditAction-validation.xml), is this XML:

XML
Code Block
XML
titleXML Validator Required String


<!DOCTYPE validators PUBLIC
 "-//OpenSymphonyApache GroupStruts//XWork Validator 1.0.23//EN"
 "http://wwwstruts.opensymphonyapache.comorg/xworkdtds/xwork-validator-1.0.23.dtd">

<validators>
 <validator type="requiredstring">
 	<param name="fieldname">personBean.firstName</param>
 	<message>First name is required.</message>
 </validator>
</validators>

...

For example if the user doesn't enter a value in the first name form field and clicks on the Save Changes button, he will see the following.

Image RemovedImage Added

Validating An Email Address

You can use the Struts 2 email validator to validate the user's input in the email field. Here is the validator node that is in the EditAction-validation.xml file.

Code Block
XMLxmlXML
xml
titleEmail Validator


 <validator type="requiredstring">
 	<param name="fieldname">personBean.email</param>
 	<message>Email address is required.</message>
 </validator>
 <validator type="email">
 	<param name="fieldname">personBean.email</param>
 	<message>Email address not valid.</message>
 </validator>

...

The Struts 2 framework provides a powerful way to validate a user's form field input by using the regex validator. In the example application, we want to ensure the user enters the phone number in the format 999-999-9999. We can use a regular expression and the regex validator to enforce this rule.

Code Block
XMLxmlXML
xml
titleREGEX Validator


<validator type="requiredstring">
 	<param name="fieldname">personBean.phoneNumber</param>
 	<message>Phone number is required.</message>
 </validator>
<validator type="regex">
	<param name="fieldname">personBean.phoneNumber</param>
	<param name="expressionregex"><![CDATA[\d{3}-\d{3}-\d{4}]]></param>
	<message>Phone number must be entered as 999-999-9999.</message>
</validator>

The param name="expression" node is used to specify the regular expression that will be applied to the user's input. Note how the regular expression is contained with within a CDATA section.

Validating A User's Input Using An OGNL Expression

In the example application, we want to ensure the user checks at least one of the car model check boxes. To enforce this rule we can use the fieldexpression validator. Here's the XML for that validator node.

Code Block
XMLxmlXML
xml
titleFieldExpression Validator


<validator type="fieldexpression">
	<param name="fieldname">personBean.carModels</param>
	<param name="expression"><![CDATA[personBean.carModels.length > 0]]></param>
	<message>You must select at least one car model.</message>
</validator>

...

In the above XML the value of the param name="expression" node, personBean.carModels.length > 0, will be evaluated by the framework as a Java statement. The part personBean.carModels tells the framework to call the getCarModels method of class Person. That method returns an Array. Since class Array has a length attribute, the framework will call get the value of the length attribute of the Array returned by the getCarModels method.

If the user did not check any of the check boxes, the Array returned by the getCarModels method will have a length value of 0. Since the complete OGNL expression will only evaluate to true if the length value is greater than 0, the validation fails. The user will see this.

Image RemovedImage Added

The fieldexpression validator is useful when doing conditional validation of a user's input. If the OGNL expression doesn't evaluate to true then the user's input won't be allowed.

...

The Struts 2 framework provides easy-to-use validation methodologies. You can add a validate method to the Action class or have a separate XML file with validation rules or you can use a combination of both methodologies.

Up Next

In our next tutorial we'll cover how to enable one action node in struts.xml to respond to several different action URLs.