WebWork 2 : Validation Examples
This page last changed on Nov 29, 2004 by jcarreira.
Included in the WW:WebWork example war file is an example of using the XW:Validation Framework in WebWork2. This example consists of three links which all use the same Action Class and view pages (Velocity). The sourcesFirst, I had to add the validators.xml file to the root of the source tree for the example app<validators> <validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/> <validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/> <validator name="date" class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/> <validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork.validator.validators.EmailValidator"/> <validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/> </validators> The Action class used by all of the validation examples is ValidatedAction package com.opensymphony.webwork.example; import com.opensymphony.xwork.ActionSupport; /** * ValidatedAction * @author Jason Carreira * Created Sep 12, 2003 9:23:38 PM */ public class ValidatedAction extends ActionSupport { private ValidatedBean bean = new ValidatedBean(); private String name; private String validationAction = "basicValidation.action"; public ValidatedBean getBean() { return bean; } public void setBean(ValidatedBean bean) { this.bean = bean; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValidationAction() { return validationAction; } public void setValidationAction(String validationAction) { this.validationAction = validationAction; } } <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <message>You must enter a name.</message> </field-validator> </field> </validators> This and all other validation files are placed in the same package as the classes to which they apply. The form for this Action is validationForm.vm <html> <head><title>Webwork Validation Example</title></head> <body> #if( $actionErrors.size() > 0 ) <p> <font color="red"> <b>ERRORS:</b><br> <ul> #foreach( $error in $actionErrors ) <li>$error</li> #end </ul> </font> </p> #end <p> <form name="myForm" action="${validationAction}" method="POST"> <input type="hidden" name="validationAction" value="${validationAction}"/> Action Properties: <br> <table> #tag( TextField "label=Name" "name=name" "value=name" ) </table> Bean Properties: #if( $stack.findValue("fieldErrors") ) #set( $beanErrors = $stack.findValue("fieldErrors.get('bean')") ) #if( $beanErrors.size() > 0 ) <br> <font color="red"> <b>Bean Errors:</b><br> <ul> #foreach( $beanError in $beanErrors ) <li>$beanError</li> #end </ul> </font> #end #end <table> #tag( TextField "label=Bean.Text" "name=bean.text" "value=bean.text" )<br> #tag( TextField "label=Bean.Date" "name=bean.date" "value=bean.date" )<br> #tag( TextField "label=Bean.Number" "name=bean.number" "value=bean.number" )<br> #tag( TextField "label=Bean.Number2" "name=bean.number2" "value=bean.number2" )<br> </table> <input type="submit" value="Test Validation"/> </form> </body> <html> <head><title>WebWork Validation Test: Valid</title></head> <body> Input was valid! </body> </html> We'll look at any other example-specific configuration files as we get to them. Basic ValidationThe BasicValidation example is defined in the example xwork.xml file like this<action name="basicValidation" class="com.opensymphony.webwork.example.ValidatedAction"> <interceptor-ref name="validationWorkflowStack"/> <result name="success" type="dispatcher">valid.vm</result> <result name="input" type="dispatcher">validationForm.vm</result> <result name="error" type="dispatcher">validationForm.vm</result> </action> The interceptor-ref here, to "validationWorkflowStack", is defined in webwork-default.xml and provides the parameter interceptors as well as the ValidationInterceptor (see XW:Validation Framework and the DefaultWorkFlowInterceptor (see XW:Interceptors#DefaultWorkflow). All of the parameters from the configuration file (there are none in this case) followed by the parameters from the request will be set onto the Action. Next, the validations will be run, and finally the DefaultWorkflow will be applied (see XW:Interceptors#DefaultWorkflow). This example is very simple, and the ValidatedAction-validation.xml file is the only set of Validations which will be applied. This means that the only validation done is that you enter some text for the name field.Visitor Validation Examplenote: check out another Visitor Field Validator Example . The ValidatedAction holds a reference to a plain Java bean, ValidatedBean:package com.opensymphony.webwork.example; import java.util.Date; /** * ValidatedBean * @author Jason Carreira * Created Sep 12, 2003 9:24:18 PM */ public class ValidatedBean { private String text; private Date date = new Date(System.currentTimeMillis()); private int number; private int number2; public static final int MAX_TOTAL = 12; public String getText() { return text; } public void setText(String text) { this.text = text; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getNumber2() { return number2; } public void setNumber2(int number2) { this.number2 = number2; } } The base validation file for the ValidatedBean is ValidatedBean-validation.xml <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <field name="text"> <field-validator type="requiredstring"> <message key="invalid.text">Empty Text!</message> </field-validator> </field> <field name="date"> <field-validator type="date"> <param name="min">01/01/1970</param> <message key="invalid.date">Invalid Date!</message> </field-validator> </field> <field name="number"> <field-validator type="int"> <param name="min">1</param> <param name="max">10</param> <message key="invalid.number">Invalid Number!</message> </field-validator> </field> </validators> <action name="visitorValidation" class="com.opensymphony.webwork.example.ValidatedAction"> <interceptor-ref name="validationWorkflowStack"/> <param name="validationAction">visitorValidation.action</param> <result name="success">valid.vm</result> <result name="input">validationForm.vm</result> <result name="error">validationForm.vm</result> </action> Here we see a slight difference from the basic validation example above. I've added a static param to the Action which will be applied to the Action by the static-param interceptor. This parameter only sets the value for the action to post the form to for validation (see validationForm.vm). The Action name above, visitorValidation is mapped to another set of validations defined in the file ValidatedAction-visitorValidation-validation.xml<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <field name="bean"> <field-validator type="required"> <message>The bean must not be null.</message> </field-validator> <field-validator type="visitor"> <message>bean: </message> </field-validator> </field> </validators> This file is found automatically by the validation framework based on the class name (ValidatedAction), the action alias, which is used as the validation context (visitorValidation) and the standard suffix (-validation.xml) to form the filename ValidatedAction-visitorValidation-validation.xml. This file defines two validators for the "bean" field, a required validator which makes sure the bean is not null, and a VisitorFieldValidator. The VisitorFieldValidator will apply the validators for the ValidatedBean using the same validation context as is used in validating ValidatedAction, visitorValidation. It therefore looks for the validation files ValidatedBean-validation.xml (the default validations for the ValidatedBean) and ValidatedBean-visitorValidation-validation.xml (the validations specific to this validation context) , in that order.The ValidatedBean-validation.xml looks like this: <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <field name="text"> <field-validator type="requiredstring"> <message key="invalid.text">Empty Text!</message> </field-validator> </field> <field name="date"> <field-validator type="date"> <param name="min">01/01/1970</param> <message key="invalid.date">Invalid Date!</message> </field-validator> </field> <field name="number"> <field-validator type="int"> <param name="min">1</param> <param name="max">10</param> <message key="invalid.number">Invalid Number!</message> </field-validator> </field> </validators> This file applies validations for three fields (text, date, and number) and gives message keys and default messages for each of them. These message keys will be used to look up messages from a resource bundle specific to the ValidatedBean class. In the same package as the ValidatedBean is a file named ValidatedBean.properties invalid.date=You must enter a date after ${min}. invalid.text=You must enter some text. invalid.number=You must enter a number between ${min} and ${max}. invalid.total=The total of number and number2 must be less than ${@com.opensymphony.webwork.example.ValidatedBean@MAX_TOTAL}. The ValidatedBean-visitorValidation-validation.xml file would define validations specific for the visitorValidation validation context, but it is not there, so it is ignored. Visitor Validation with the Expression ValidatorThe final example shows a similar setup to the previous visitor validation example. The xwork.xml configuration for this example is very similar to the visitorValidation example:<action name="expressionValidation" class="com.opensymphony.webwork.example.ValidatedAction"> <interceptor-ref name="validationWorkflowStack"/> <param name="validationAction">expressionValidation.action</param> <result name="success">valid.vm</result> <result name="input">validationForm.vm</result> <result name="error">validationForm.vm</result> </action> The ValidatedAction-expressionValidation-validation.xml file defines the validations specific to this validation context: <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <field name="bean"> <field-validator type="required"> <message>The bean must not be null.</message> </field-validator> <field-validator type="visitor"> <param name="context">expression</param> <message>bean: </message> </field-validator> </field> </validators> In this case, the validation context specific validations for the ValidatedBean is present, and it's named ValidatedBean-expression-validation.xml <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <validator type="expression"> <param name="expression">@com.opensymphony.webwork.example.ValidatedBean@MAX_TOTAL > (number + number2)</param> <message key="invalid.total">Invalid total!</message> </validator> </validators> This adds an object-level (as opposed to field-level) ExpressionValidator which checks the total of the number and number2 fields against a static constant, and adds an error message if the total is more than the constant. A note about error messages with the VisitorFieldValidatorWith the VisitorFieldValidator, message field names are appended with the field name of the field in the Action. In this case, the fields "text", "date", and "number" in the ValidatedBean would be added as field error messages to the Action with field names "bean.text", "bean.date", and "bean.number". The error messages added for the object-level ExpressionValidator applied in the last example will be added as field-level errors to the Action with the name "bean". |
![]() |
Document generated by Confluence on Dec 14, 2004 16:36 |