首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2 数据校验小结

2012-09-09 
struts2 数据校验总结struts2的数据校验实在struts2的类型转换之后进行的,给出如下的示例:?1、编写register

struts2 数据校验总结

struts2的数据校验实在struts2的类型转换之后进行的,给出如下的示例:

?1、编写register2.jsp,让客户输入数据,代码如下:

??

<body><table align="center" width="30%"><tr><td style="color:red"><s:fielderror></s:fielderror></td></tr></table><form action="reg.action" method="post"><table align="center" border="1" width="30%"><tr>             <td>username</td><td><input type="text" name="username" /></td></tr><tr><td>password</td><td><input type="password" name="password"></td></tr><tr><td>re-password</td><td><input type="password" name="repassword"></td></tr><tr><td>age</td><td><input type="text" name="age"></td></tr><tr><td>birthday</td><td><input type="text" name="birthday"></td></tr><tr><td>graduation</td><td><input type="text" name="graduation"></td></tr>                <tr><td>    <input type="submit" value="submit"></td><td><input type="reset" value="reset"></td></tr></table>                       </form></body>

?? 可以按照如下的规则进行数据校验

?? (1)username不能为空值,并且长度在6--10之间。

?? (2)password不能为空值,并且长度在6--10之间。

?? (3)repassword不能为空值,并且长度在6--10之间。

?? (4)age必须在1--150之间

???(5)birthday不能为空值。

?? (6)graduation不能为空值。

?? (7)birthday的日期必须早于graduation的日期。

2、配置校验文件,命名为:RegisterAciton-validation.xml。加入如下代码

???

<?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 is should not be null!</message>   </field-validator>   <field-validator type="stringlength">   <param name="maxLength">10</param>   <param name="minLength">6</param>   <message>username length is incorrect and should be between ${minLength} and ${maxLength}</message>   </field-validator></field><field name="password"><field-validator type="requiredstring">  <param name="trim">true</param>  <message>password is should not be null</message></field-validator><field-validator type="stringlength"><param name="maxLength">10</param><param name="minLength">6</param><message>password length is incorrect and should be between ${minLength} and ${maxLength}</message></field-validator></field><field name="repassword"><field-validator type="requiredstring">  <param name="trim">true</param>  <message>repassword is should not be null</message></field-validator><field-validator type="stringlength"><param name="maxLength">10</param><param name="minLength">6</param><message>repassword length is incorrect and should be between ${minLength} and ${maxLength}</message></field-validator></field><field name="age">    <field-validator type="int">    <param name="max">10</param>    <param name="min">6</param>    <message>age is incorrect and should be between ${min} and ${max}</message>    </field-validator></field>  <field name="birthday">   <field-validator type="required">    <message>birthday is incorrect</message>   </field-validator> </field> <field name="graduation">   <field-validator type="required">    <message>graduation is incorrect</message>   </field-validator>   <field-validator type="date">   <param name="max">2050-08-12</param>   <param name="min">2010-09-23</param>   <message>graduation should after the birthday</message>   </field-validator> </field></validators>

?3、编写RegisterAciton类

???

package com.test.action;import java.util.Calendar;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class RegisterAciton extends ActionSupport{private String username;private String password;private String repassword;private int age;private Date birthday;private Date graduation;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getRepassword() {return repassword;}public void setRepassword(String repassword) {this.repassword = repassword;}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;}@Overridepublic String execute() throws Exception {  return SUCCESS;}@Overridepublic void validate() {}public Date getGraduation() {return graduation;}public void setGraduation(Date graduation) {this.graduation = graduation;}}

?

?? 4、配置struts.xml文件

?????

<action name="reg" name="code"><body>    username <s:property value="username"/><br>    password <s:property value="password"/><br>    re-password <s:property value="repassword"/><br>    age <s:property value="age"/><br>    birthday <s:property value="birthday"/><br>    graduation <s:property value="graduation"/></body>

?至此一个数据校验实例配置成功了

6、总结

??? (1)数据校验中xml文件的命名必须准确”action的类名-validation.properties“。

??? (2)struts2为我们提供了默认的校验,打印出的信息很不友好,所以我们可以对它提供的信息进行修正,有以下两种方法:

?? 第一种方法:

??? 在struts.xml中加入

?????????

<constant name="struts.custom.i18n.resources"value="message"></constant>

??? 然后新建一个名为message.properties的文件,代码如下

????

xwork.default.invalid.fieldvalue={0} error

??第二种方法:

????? 定义文件名为“类名.properties”输入的代码如下

?????

invalid.fieldvalue.age=\u5e74\u9f84\u8f93\u5165\u4e0d\u6b63\u786e

???? 意思是:invalid.fieldvalue.age=“年龄信息输入不正确”。

? (3)总共有两种校验方式,addActionError方法和addFieldError方法,可以在jsp文件中进行如下设置,来指定要显示的内容。

??

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

?(4)除了服务器端的数据校验我们可以提供客户端的数据校验,客户段数据校验有两种方法

???? 第一种方法: form的主题(theme)一定不能设置为simple;form的validate属性设为true
??????????????????????? 最好不要使用struts2给我们提供的客户端校验方法。

??? 第二种方法:编写js代码实现

??????????????????????? 将form的onsubmit的值设为如下的代码

?????????????????????????????

<s:form action="reg" theme="simple" onsubmit="return validate()">

????? 编写js

???????

 <script type="text/javascript">       function validate()       {         var usernamevalue=document.getElementById("usernameId").value;         var passwordvalue=document.getElementById("passwordId").value;         var repasswordvalue=document.getElementById("repasswordId").value;         if(usernamevalue.length==0)         {             alert("username should not be blank");             return false;           }         else if(usernamevalue.length>10||usernamevalue.length<6)          {             alert("length of username should be between 6 and 10");             return false;              }               if(passwordvalue.length==0)         {             alert("password should not be blank");             return false;           }         else if(passwordvalue.length>10||passwordvalue.length<6)          {             alert("length of password should be between 6 and 10");             return false;              }                  if(repasswordvalue.length==0)         {             alert("repassword should not be blank");             return false;           }         else if(repasswordvalue.length>10||repasswordvalue.length<6)          {             alert("length of repassword should be between 6 and 10");             return false;              }                     if(repasswordvalue!=passwordvalue)              {             alert("repasswordis not equals password!");             return false;              }           return true;       }          </script>

?(5)当我们使用xml文件对数据进行校验时,在action类中不要重写validate方法。

(6)我们可以只对具体action类中的某个业务逻辑进行校验,命名方式为“aciton类的名字-方法名-validation.xml”,如果已经定义了"action类的名字-validation.xml",那么struts2会先执行后者在执行前者。所以我们最好不要定义后者

(7)

???

class parentAction{   test()}  class childAction extends parentAction{test()//重写test}parentAction-validtion.xmlparentAction-test-validtion.xmlchildAction-validtion.xmlchildAction-test-validtion.xml

?对于以上代码,struts2对所有xml文件的访问顺序为从上到下。

?

?????????????????????????

?????????????

热点排行