Struts 2.0 relies on a validation framework provided by XWork input validation. Along with basic validation and client-side Javascript validation offered in Struts 1.x, Struts 2 offers Ajax based validation. The following example demonstrates how to use Struts 2 validation, both basic and ajax validations. For this, the sample page used is shown below.
<s:form action="RegisterUser">The Action definition is shown below:
<s:textfield name="userName" size="20" label="User Name" />
<s:textfield name="emailAddress" size="20" label="Email Address" />
<s:textfield name="dateOfBirth" size="20" label="Date Of Birth" />
<s:submit name="submit" value="submit" />
</s:form>
<action name="RegisterUser" method="registerUser" class="example.RegisterUser">Based on above definition, the Action class must have a method name registerUser.
<result name="input">/example/Register.jsp</result>
<result>/example/HelloWorld.jsp</result>
</action>
Basic Validation
For basic validaton, you have to define the validation rules in
<validators>Note that the "date of birth" field has two validators associated with it. The "regex" validator type takes a parameter by the name "expression" which is the regular expression used to validate the field. The message keys used in validation rules must be defined in "package.properties" file as follows:
<field name="userName">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>
<field name="emailAddress">
<field-validator type="email">
<message key="fieldFormat" />
</field-validator>
</field>
<field name="dateOfBirth">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
<field-validator type="regex">
<param name="expression">
[0-9][0-9]/[0-9][0-9]/[1-9][0-9][0-9][0-9]
</param>
<message key="fieldFormat" />
</field-validator>
</field>
</validators>
requiredstring = ${getText(fieldName)} is required.
fieldFormat = ${getText(fieldName)} is not formatted properly.
Client Side Validation
For simple client-side validation without Ajax, just add a validate="true" to the form definition in the JSP, as follows:
<s:form action="RegisterUser" validate="true">Also note that the message keys do not work(atleast not for me), and you may have to define the error messages directly instead of through the properties file as follows:
<message>Date of birth is not formatted properly</message>
Ajax Validation
Struts implements Ajax Validation by using DWR. For a quick start of DWR read Ajax in Java with DWR. Coming to Struts validation, follow these steps to setup DWR
- Download DWR from here.
- Add DWR servlet mapping in the web deployment descriptor as shown below
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping> - In your WEB-INF directory, create a dwr.xml file and declare the struts validator as follows
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="new" javascript="validator">
<param name="class"
value="org.apache.struts2.validators.DWRValidator" />
</create>
<convert converter="bean"
match="com.opensymphony.xwork2.ValidationAwareSupport" />
</allow>
<signatures>
<![CDATA[
import java.util.Map;
import org.apache.struts2.validators.DWRValidator;
DWRValidator.doPost(String, String, Map<String, String>);
]]>
</signatures>
</dwr> - Change the form declaration in your JSP file to include "theme=ajax" as shown below
<s:form action="RegisterUser" validate="true" theme="ajax">