Section 19.9. DynaActionForms and the Struts Validator


19.9. DynaActionForms and the Struts Validator

After the early versions of Struts were released, it became clear that there were certain ActionForm idioms that were so conventional, it would be helpful to introduce ways to declare such ActionForms without requiring Java code to be written. For example, most ActionForms are nothing more than ordinary JavaBeans with getters and setters for very common property types: Strings, ints, Booleans, and so forth. Similarly, validations observed certain conventions: some fields were required, others were required and needed to be sequences of digits forming integers, and so forth.

Enter DynaActionForms and the Struts validator .

org.apache.struts.action.DynaActionForm is a subclass of ActionForm that creates properties on the fly through the Java Reflection API. In your Struts config file, you simply declare that your form is bound to a DynaActionForm, and then write XML to define the properties. Instead of accessing the properties through explicit method calls (e.g., getUsername( )), you call a generic get method with the name of the property (for example, get("Username")).

We will focus on a subclass of DynaActionForm that provides for writing even less code (or more XML!) called org.apache.struts.validator.DynaValidatorForm. With DynaValidatorForm, we can not only declare our properties in XML, we can also define standard validations that will be exercised by the Struts validator (a Struts PlugIn).

The example application provides two versions of the LoginForm. One version is implemented through Java code; the other is implemented through declarations in the Struts configuration file, binding the form to an instance of org.apache.struts.validator.DynaValidatorForm. To observe the difference between the Java implementation of the LoginForm and the dynamic version, you will need to look at the comments in struts-config.xml, LoginAction.java, and index.jsp. In each of these three files, there are lines to be commented in or out depending on whether you want to use the Java implementation of LoginForm or the dynamic version. Additionally, the dynamic version depends on three more files: validator-rules.xml, validation.xml, and application.properties.

For the DynaValidatorForm version, the LoginForm is defined in struts-config.xml:

 <form-bean name="LoginForm"     type="org.apache.struts.validator.DynaValidatorForm">   <form-property name="username" type="java.lang.String"/>   <form-property name="password" type="java.lang.String"/> </form-bean> 

Voila! You have just saved yourself about 30 lines of Java code.[7] LoginAction.java requires a bit of tweaking, because now the username and password values are retrieved through the generic get method on DynaValidatorForm:

[7] For another example of using a DynaValidatorForm, see the comments in struts-config.xml regarding the form bean BookDisplayForm. For this example, we have to extend DynaValidatorForm to handle checkbox values.

 DynaActionForm loginForm = (DynaActionForm) form; String username = (String) loginForm.get("username"); String password = (String) loginForm.get("password"); 

The final components you will need to look at are the two configuration files validation.xml and validator-rules.xml, which work together, and index.jsp. The validator-rules.xml file is a standard file that comes with Struts. You add it the web archive under /WEB-INF; you will not have to change this file unless you are introducing validations of your own. The validation.xml file contains your requirements for validation; it also goes under /WEB-INF. References to both of these files must be present in the declaration of the Struts validator plug-in in struts-config.xml:

 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">   <set-property     property="pathnames"     value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> 

The validation.xml file is where you define the mappings between your form fields and the validations that are required for them. Here's an extract of our validation.xml that defines the validations for the LoginForm (for the full story on the structure of this file and the elements that comprise a validation declaration, see Chapter 11 in O'Reilly's Programming Jakarta Struts):

 <form name="LoginForm">   <field property="username" depends="required">     <msg name="required" key="errors.required"/>     <arg0 key="login.username"/>   </field>   <field property="password" depends="required">     <msg name="required" key="errors.required"/>     <arg0 key="login.password"/>   </field> </form> 

In this extract from validation.xml, we specify the validations for the fields username and password of the form LoginFormthe depends attribute of the <field> element specifies the names of the validations that must be executed when the request processor calls the validator( ) method on the ActionForm (i.e., the DynaValidatorForm). The <msg> element defines the name of the key in application.properties that should be used as an error message template when the validation fails. Thus, in application.properties, we have defined the property errors.required as {0} is required. The <arg0> element provides the key for the name of the field to fill into the {0} in the error message template.

We have left the last bit of magic for last. The Struts validator plug-in provides for client-side validation (through JavaScript) as well as server-side validation. To enable this, you must provide some additional code in the JSP. To the <html:form> tag, you must add an onsubmit attribute to ensure that the appropriate dynamically generated JavaScript is called for the particular form. The revised <html:form> tag looks like this:

 <html:form action="/login" focus="username"     onsubmit="return validateLoginForm(this);"> 

And to generate the JavaScript itself, we add, after the </html:form> close tag, an <html:javascript> tag:

 <html:javascript formName="LoginForm"/> 

In "The ActionForm and the JSP" earlier in this chapter, we discuss routine validation through the validate( ) method that may be overridden when implementing an ActionForm. In the default implementation, if you enter nothing for your credentials during login, you will see error messages in the page. These messages are the results of server-side validation conducted by the Java implementation.

Now when you attempt to log in with empty credentials, you should see validation messages in the form of JavaScript pop-ups. Thanks to the Struts validator plug-in, you have just saved yourself the pain of writing and debugging JavaScript.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net