Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Ajax validation with Struts 2

Support for Ajax and JavaScript takes the pain out of Web-form validation

  • Print
  • Feedback

Page 5 of 6

Plain client-side validation

It's possible to validate forms on the client side using Struts 2. JavaScript client-side validation is configured exactly the same way as the server-side validation; no special configuration is required. The key point of client-side validation is that it is handled by JavaScript validators instead of Java XWork ones. The list of available JavaScript validators is shorter than the server-side list. It is predefined and can't be expanded with reasonable effort.

Client-side validation is triggered with additional validate attribute in the <form> tag:

<s:form action="First_" theme="xhtml" validate="true">

So, what happens in our use case with the validation-configuration XML file containing validators that are available in JavaScript (requiredstring and int) and those implemented on the server-side only (fieldexpression and gender)? Struts 2 validates the form on the client side with validators implemented in JavaScript, then passes the validation process to the remaining validators on the server side.

Ajax validation

Ajax validation combines the power of server-side and plain client-side validation. Advanced server-side validators can be used without submitting the form or reloading the Web page. In Struts 2.0.x, Ajax validation depends on Direct Web Remoting (DWR). Keep in mind that Struts 2.1 (in beta as of this writing) will remove DWR dependency.

DWR needs to be set up in two steps. First, you must register the DWR servlet in web.xml, as shown in Listing 12:

Listing 12. Servlet configuration: DWR

 <servlet>
   <servlet-name>dwr</servlet-name>
   <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>dwr</servlet-name>
   <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

Then, the DWR JAR file (not a part of the Struts 2 distribution but present in some Struts 2 example applications) must be present in WEB-INF/lib. The last DWR set-up step is to put the dwr.xml DWR configuration file in the WEB-INF folder. Listing 13 shows the contents of drw.xml:

Listing 13. dwr.xml

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

All that's required to make Ajax validation work now is to change the theme to ajax in the <form>:

<s:form action="First_" theme="ajax" validate="true">

Field validation is triggered once focus leaves the field input, and nonfield validation (the type that produces action errors) is triggered on form submit.

  • Print
  • Feedback

Resources

More