欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2学习笔记八(第8讲.Struts2的校验框架)

程序员文章站 2022-05-28 15:53:53
...
[b][size=xx-large]第8讲.Struts2的校验框架[/size][/b]

之前的验证方式是struts2提供的硬编码的方式的验证,struts2还提供了一个优秀的校验框架,是基于xml文件配置的,我们可以通过这个xml配置文件,而不用再写任何的代码就能完成表单的输入校验。
一、如果要验证哪个action就要在那个action的同一目录下创建验证框架的xml的文件,还是基于上节课的register2.jsp的表单验证,所以在com.test.action包下创建RegisterAction-validation.xml文件(后面的-validation.xml是不变的,前面的是对应的要验证的action的名称):

<?xml version="1.0" encoding="UTF-8"?>
<!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="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username should not be blank!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>

<field name="password">
<field-validator type="requiredstring">
<message>password should not be blank!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>

<field name="repassword">
<field-validator type="requiredstring">
<message>repassword should not be blank!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>repassword should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>

<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>

<field name="birthday">
<field-validator type="required">
<message>birthday should not be blank!</message>
</field-validator>
<field-validator type="date">
<param name="min">2001-01-01</param>
<param name="max">2003-12-31</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>

<field name="graduation">
<field-validator type="required">
<message>graduation should be not blank!</message>
</field-validator>
<field-validator type="date">
<param name="min">2005-01-01</param>
<param name="max">2007-12-31</param>
<message>graduation should be between ${min} and ${max}</message>
</field-validator>
</field>

</validators>


三、同时注释掉action类中的对应的自定义的验证方法,注释后代码如下:

...
@Override
public String execute() throws Exception {

return SUCCESS;
}
// public String abc() throws Exception {
//
// System.out.println("abc() method invoked");
// return SUCCESS;
// }
//
// public void validateAbc(){
//
// System.out.println("validateAbc() invoked");
// }

@Override
public void validate() {

System.out.println("validate~~~~~~~~~~~~~~");
if(null == username || username.length() < 6 || username.length() > 10){
this.addActionError("username invalid");
}
if(null == password || password.length() <6 || password.length() > 10){
this.addActionError("password invalid");
}
else if(null == repassword || repassword.length() < 6 || repassword.length() > 10){
this.addActionError("repassword invalid");
}
else if(!password.equals(repassword)){
this.addFieldError("password", "two passwords not the same");
}
if(age <= 0 || age > 150){
this.addActionError("age should be between 1 and 150");
}

if(null != birthday && null != graduation){
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);

Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);

if(!c1.before(c2)){
this.addActionError("birthday should be before graduation");
}
}
}
...

四、重新修改Register2.jsp页面中的显示错误信息的级别,因为之前的是actionerror级别的,要修改为fielderror级别的:

<table align="center" width="40%">
<tr>
<td>
<s:fielderror cssStyle="color:red"/>
</td>
</tr>
</table>

说明:如果校验信息不是很复杂的情况下尽量的使用校验框架,如果校验信息十分复杂的情况下要用validate()进行校验,目的都是将错误信息放到fielderror级别里面去。
相关标签: 框架 XML JSP