Commits

jcarreira  committed 1b880b1

Updating to latest XWork and adding Validation example to example war

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@44573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 79af601

Comments (0)

Files changed (15)

File lib/core/xwork-1.0-beta.jar

Binary file modified.

File src/etc/example/xwork.xml

 
         <action name="exception" class="com.opensymphony.webwork.example.ExceptionAction">
         </action>
+
+        <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>
+
+        <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>
+
+        <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>
     </package>
 </xwork>

File src/example/com/opensymphony/webwork/example/ValidatedAction-expressionValidation-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">
+            <param name="context">expression</param>
+            <message>bean: </message>
+        </field-validator>
+    </field>
+</validators>

File src/example/com/opensymphony/webwork/example/ValidatedAction-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="name">
+        <field-validator type="requiredstring">
+            <message>You must enter a name.</message>
+        </field-validator>
+    </field>
+</validators>

File src/example/com/opensymphony/webwork/example/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>

File src/example/com/opensymphony/webwork/example/ValidatedAction.java

+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;
+    }
+}

File src/example/com/opensymphony/webwork/example/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>

File src/example/com/opensymphony/webwork/example/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>

File src/example/com/opensymphony/webwork/example/ValidatedBean.java

+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;
+    }
+}

File src/example/com/opensymphony/webwork/example/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}.

File src/example/validators.xml

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

File src/java/webwork-default.xml

 <xwork>
     <package name="webwork-default">
         <result-types>
-            <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult"/>
+            <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
             <result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
             <result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
             <result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/>

File src/webapp/default.jsp

 Click <a href="showVelocityForm.action">here</a> for an example of the Form Tag and the Token Tag for hidden transaction tokens <strong>using Velocity as the view</strong><br>
 Click <a href="form2.jsp">here</a> for an example of the Form Tag and the Token Tag for hidden transaction tokens. This example shows you the original result rather than an error (TokenSessionInterceptor).<br>
 Click <a href="exception.action">here</a> for an example of what happens when exceptions are thrown<br>
-Click <a href="config-browser/namespace.action">here</a> for the WebWork configuration browser
+Click <a href="config-browser/namespace.action">here</a> for the WebWork configuration browser<br>
+Click <a href="basicValidation.action">here</a> for a basic validation example<br>
+Click <a href="visitorValidation.action">here</a> for a visitor validation example<br>
+Click <a href="expressionValidation.action">here</a> for a visitor validation example with expression validation<br>

File src/webapp/valid.vm

+<html>
+<head><title>WebWork Validation Test: Valid</title></head>
+<body>
+Input was valid!
+</body>
+</html>
+
+
+

File src/webapp/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>