当前位置: 代码迷 >> Ajax >> 转: 数据校验效能-AJAX输入校验(9)中
  详细解决方案

转: 数据校验效能-AJAX输入校验(9)中

热度:274   发布时间:2012-07-19 16:02:19.0
转: 数据校验功能-AJAX输入校验(9)中

7.4.2??建立业务控制器

建立一个简单的业务控制器实现类,如代码7.21所示。

代码7.21??AJAX校验示例的业务控制器

?

package?ch7;

import?java.util.Date;

import?com.opensymphony.xwork2.ActionSupport;

public?class?Reg_ajax_Action?extends?ActionSupport?{

????
private?String?name;

????
private?int?age;

????
private?Date?birthday;

????
//属性的getter和setter方法

?
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?Date?getBirthday()?{

????????
return?birthday;

????}


????
public?void?setBirthday(Date?birthday)?{

????????
this.birthday?=?birthday;

????}


}


?

Action非常简单,只是定义了3个相应的属性。在配置文件中加入如下内容:

<action name="Reg_ajax_Action" class="ch7.Reg_ajax_Action">

????????????<result name="input">/ch7/reg-ajax.jsp</result>

</action>

7.4.3??建立校验规则文件

建立一个Reg_ajax_Action-validation.xml校验规则文件,如代码7.22所示。

代码7.22??AJAX校验示例的校验规则文件

?

<!DOCTYPE?validators?PUBLIC?"-//OpenSymphony?Group//XWork?Validator?1.0.2//EN"?"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<!--

????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>用户名不能为空!</message>

????????
</field-validator>

????
</field>

????
<field?name="age">

????????
<field-validator?type="int">

????????????
<param?name="min">13</param>

????????????
<param?name="max">19</param>

????????????
<message>年龄必须在13至19岁之间</message>

????????
</field-validator>

????
</field>

????
<field?name="birthday">

????????
<!--定义类型为date-->

????????
<field-validator?type="date">

????????????
<!--校验生日有效时间段-->

????????????
<param?name="min">1990-01-01</param>

????????????
<param?name="max">2006-01-01</param>

????????????
<message>生日必须在${min}至${max}之间</message>

????????
</field-validator>

????
</field>

</validators>

?

?说明?

该校验规则文件也是一个标准的校验规则文件。

  相关解决方案