首页 > 代码库 > Struts2中的验证框架
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中的验证框架
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。