JavaScud | Forum | JIRA | Blog |
  Dashboard > WebWork2文档中文化计划 > ... > Validation > Basic Validation
  WebWork2文档中文化计划 Log In View a printable version of the current page.  
  Basic Validation
Added by scud, last edited by scud on Apr 30, 2006  (view change)
Labels: 
(None)

说明

下列步骤是在Webwork中配置基本的校验.

第一步:

写一个html表单.

<html>
<head>
    <title>Validation - Basic</title>
    <ww:head/>
</head>

<body>

<b>What is your favorite color?</b>
<p/>

<ww:form method="post">
    <ww:textfield label="Name" name="name"/>
    <ww:textfield label="Age" name="age"/>
    <ww:textfield label="Favorite color" name="answer"/>
    <ww:submit/>
</ww:form>

</body>
</html>

第二步

写一个action类

public class QuizAction extends ActionSupport {
    String name;
    int age;
    String answer;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

第三步:

填写要使用的校验器.validation.xml的格式是 <ActionClassName>-validation.xml 或者 <ActionClassName>-<ActionContextName>-validation.xml .

<!--
    Add the following DOCTYPE declaration as first line of your XXX-validation.xml file:
    <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-->
<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message>You must enter a name</message>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="int">
            <param name="min">13</param>
            <param name="max">19</param>
            <message>Only people ages 13 to 19 may take this quiz</message>
        </field-validator>
    </field>
</validators>

fg

Posted by Anonymous at Nov 08, 2006 08:50 | Reply To This
Site running on a free Atlassian Confluence Open Source Project License granted to WebWork China. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.3 Build:#808 May 29, 2007) - Bug/feature request - Contact Administrators