码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2中的验证框架

时间:2016-11-27 06:15:55      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:目录   sam   str   key   support   system   定义   post   国际化   

通过注解的方式,可以让方法不用验证

@SkipValidation
public String toRegView() {
   System.out.println("toRegView");
   return "regView";
}

 

public class ValidateAction extends ActionSupport {
    private static final long serialVersionUID = -6933309304624396640L;

    private String name;
    private Integer age;
    private String password ;
    private String confirmPassword ;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String reg() {
        return "success";
    }

    @SkipValidation
    public String toRegView() {
        System.out.println("toRegView");
        return "regView";
    }
}

 

ActionName-validation.xml

<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message>name is required</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">5</param>
            <param name="maxLength">10</param>
            <message key="error.name.length" />
        </field-validator>
        <field-validator type="email">
            <message>name is invalid email address!</message>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="required">
            <message>age is required</message>
        </field-validator>
        <field-validator type="int">
            <param name="min">20</param>
            <param name="max">100</param>
            <message>age rang is wrong!</message>
        </field-validator>
    </field>
    <validator type="expression">  注意这里不是进行字段的校验,所以不用field,用validator
        <param name="expresssion">password == confirmPassword</param>  验证密码是否一致
        <message>password input isn‘t the same!</message>
    </validator>
</validators>

 

考虑到国际化的关系,如果把验证的错误提示写在验证的配置文件里,这样就没办法做到国际化

此时应该把错误提示放到一个配置文件中。

在action同目录下,新建一个配置文件,命名规则为:ActionName.properties

error.name.length=${name} is goog name,but length must be between ${minLength} and ${maxLength}.
在配置文件中定义名字长度错误的信息

 

此时还需要把此处定义信息的key与验证配置文件关联:

<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message>name is required</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">5</param>
            <param name="maxLength">10</param>
            <message key="error.name.length" />这里的key与上面关联
        </field-validator>
    </field>
</validators>

 

 

<html>
    <head>
        <title>reg.jsp</title>
    </head>
    <body>
        <s:actionerror/> 这句是显示密码是否一致的错误消息,与字段错误的消息提示不同
        <s:form namespace="/validate" action="ValidateAction_reg" method="post" theme="xhtml">
            <s:textfield name="name" label="UserName" />
            <s:textfield name="age" label="UserAge" />
            <s:textfield name="password" label="password" />
            <s:textfield name="confirmPassword" label="confirmPassword" />
            <s:submit />
        </s:form>
    </body>
</html>

 

 

注意:上面的校验是服务端校验,如果想让客户端校验,可以在form表单加一个属性

<html>
    <head>
        <title>reg.jsp</title>
    </head>
    <body>
        <s:actionerror/>
        <s:form namespace="/validate" action="ValidateAction_reg" method="post" theme="xhtml" validate="true">  加这个属性,就是客户端校验
            <s:textfield name="name" label="UserName" />
            <s:textfield name="age" label="UserAge" />
            <s:textfield name="password" label="password" />
            <s:textfield name="confirmPassword" label="confirmPassword" />
            <s:submit />
        </s:form>
    </body>
</html>

 

Struts2中的验证框架

标签:目录   sam   str   key   support   system   定义   post   国际化   

原文地址:http://www.cnblogs.com/lxboy2009/p/6105525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!