Struts2中的验证框架

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2中的验证框架相关的知识,希望对你有一定的参考价值。

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

@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中的验证框架的主要内容,如果未能解决你的问题,请参考以下文章

struts2:字段校验和非字段校验代码示例

Struts2框架使用之struts2的验证框架

struts2学习(11)struts2验证框架1.验证简介内置验证

Struts2快速后台验证 使用

struts2入门——登陆验证熟悉struts2部署

自己定义验证器——用Struts2框架以框架师的思维灵活做好该事情