posts - 64,  comments - 9,  trackbacks - 0

          一. ActionSupport是個(gè)工具類,他實(shí)現(xiàn)了Action, Validatable等接口, Validatable提供validate()方法進(jìn)行數(shù)據(jù)驗(yàn)證.Action只要繼承ActionSupport類,重寫(xiě)validate()方法就可以進(jìn)行數(shù)據(jù)驗(yàn)證

          二. 校驗(yàn)的流程
               首先,Struts框架對(duì)輸入數(shù)據(jù)進(jìn)行類型轉(zhuǎn)換,然后再進(jìn)行數(shù)據(jù)校驗(yàn),如果類型轉(zhuǎn)換與數(shù)據(jù)校驗(yàn)都沒(méi)有錯(cuò)誤發(fā)生, 就進(jìn)入execute(),否則請(qǐng)求將被轉(zhuǎn)發(fā)到input視圖

          三. 注冊(cè)實(shí)例
              首先新建RegistAcion.java

          Java代碼 復(fù)制代碼
          1. package com;   
          2. import java.util.Date;   
          3. import com.opensymphony.xwork2.ActionSupport;   
          4. public class RegistAction extends ActionSupport {   
          5.     private String userName;   
          6.            
          7.     private Integer age;   
          8.        
          9.     private Date birthday;   
          10.        
          11.     public String getUserName() {   
          12.         return userName;   
          13.     }   
          14.     public void setUserName(String userName) {   
          15.         this.userName = userName;   
          16.     }   
          17.     public Integer getAge() {   
          18.         return age;   
          19.     }   
          20.     public void setAge(Integer age) {   
          21.         this.age = age;   
          22.     }   
          23.     public Date getBirthday() {   
          24.         return birthday;   
          25.     }   
          26.     public void setBirthday(Date birthday) {   
          27.         this.birthday = birthday;   
          28.     }   
          29.     @Override  
          30.     public String execute() throws Exception {   
          31.         System.out.println("注冊(cè)成功");   
          32.         return SUCCESS;   
          33.     }   
          34.     @Override  
          35.     public void validate() {   
          36.         if("".equals(userName)){   
          37.             addFieldError("userName""username is empty");   
          38.         }   
          39.         if(null != age){   
          40.             if(1 > age || 150 < age){   
          41.                 addFieldError("age""age invalid");   
          42.             }   
          43.         }   
          44.     }   
          45. }  

           配置Action

          Xml代碼 復(fù)制代碼
          1. <action name="regist" class="com.RegistAction">  
          2.       <result name="success">/welcome.jsp</result>  
          3.       <result name="input">/regist.jsp</result>  
          4.  </action>  

           接著是注冊(cè)頁(yè)面和注冊(cè)成功頁(yè)面

          regist.jsp

          Html代碼 復(fù)制代碼
          1. <body>  
          2.     <form action="regist.action" method="post">  
          3.         <s:fielderror></s:fielderror>  
          4.         <table><tr>  
          5.                 <td>userName:</td>  
          6.                 <td>  
          7.                     <input type="text" name="userName">  
          8.                 </td>  
          9.             </tr>  
          10.             <tr>  
          11.                 <td>age:</td>  
          12.                 <td>  
          13.                     <input type="text" name="age">  
          14.                 </td>  
          15.             </tr>  
          16.             <tr>  
          17.                 <td>birthday:</td>  
          18.                 <td>  
          19.                     <input type="text" name="birthday">  
          20.                 </td>  
          21.             </tr>  
          22.             <tr>  
          23.                 <td colspan="2">  
          24.                     <s:submit value="注冊(cè)"></s:submit>  
          25.                 </td>  
          26.             </tr>  
          27.     </form>  
          28.   </body>  

           如果不輸入userName, age輸入為abc,會(huì)提示
           Invalid field value for field "age".
           username is empty

          1. 其中Invalid field value for field "age" 信息是struts2通過(guò)內(nèi)置的類型轉(zhuǎn)換器進(jìn)行類型轉(zhuǎn)換時(shí),如果不能成功轉(zhuǎn)換, struts2框架自動(dòng)生成一條錯(cuò)誤信息,并將該錯(cuò)誤信息放到addFieldError里面,這種默認(rèn)的輸出信息格式是在  xwork-2.0.4.jar中定義的.  com/opensymphony/xwork2/xwork-messages.properties文件中有一條xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".

          2. 這是一種全局的錯(cuò)誤提示方式,整個(gè)系統(tǒng)中只要是字段類型轉(zhuǎn)換錯(cuò)誤都會(huì)這樣提示,我們也可以改變這種輸出格式,只要在全局的國(guó)際資源文件中重寫(xiě)xwork.default.invalid.fieldvalue就可以了.

          實(shí)現(xiàn)方式:
          在struts.xml中加入<constant name="struts.custom.i18n.resources" value="messageResource"></constant> (此處i18n,不是l,是1)
          或者也可以在struts.properties中加入struts.custom.i18n.resources=messageResource
          指定國(guó)際化資源文件名為messageResource. Properties

          新建messageResource. Properties資源文件并添加數(shù)據(jù)xwork.default.invalid.fieldvalue={0} failure
          修改之后字段類型轉(zhuǎn)換錯(cuò)誤提示為 : {0} failure

          3 所有的類型轉(zhuǎn)換失敗后,struts2會(huì)將基本類型設(shè)置為0,對(duì)象類型設(shè)置為null,這里的age的類型為Integer,當(dāng)類型轉(zhuǎn)換失敗age值為null,如果age的類型為int,那么轉(zhuǎn)換失敗后值為0

          4.這種提示信息不夠友好,也可以定義局布的提示信息,為每一個(gè)Action新建一個(gè)properties文件,文件名為XXX.properties(Action名.properties)

          實(shí)現(xiàn)方式:新建RegistAction.properties并添加
          invalid.fieldvalue.age=age error
          invalid.fieldvalue.birthday=birthday error
          其中age和birthday分別為字段的名稱

          四.
          Struts2也提供類似BaseDispatchAction的功能

          Java代碼 復(fù)制代碼
          1. package com;   
          2. import com.opensymphony.xwork2.ActionSupport;   
          3. public class Regist2Action extends ActionSupport {   
          4.     private String userName;   
          5.            
          6.     public String getUserName() {   
          7.         return userName;   
          8.     }   
          9.     public void setUserName(String userName) {   
          10.         this.userName = userName;   
          11.     }   
          12.     public String regist() throws Exception {   
          13.         System.out.println("注冊(cè)成功-regist");   
          14.         return SUCCESS;   
          15.     }   
          16.        
          17.     public void validateRegist() {   
          18.         if(userName.equals("")){   
          19.             addFieldError("userName""請(qǐng)輸入用戶名-registValidate");   
          20.         }   
          21.     }   
          22. }  

            <action name="regist2" class="com.Regist2Action" method="regist">
               <result name="success">/welcome.jsp</result>
               <result name="input">/regist2.jsp</result>
             </action>

          指定了method為regist,當(dāng)請(qǐng)求時(shí)會(huì)執(zhí)行regist(),不會(huì)再去執(zhí)行默認(rèn)的execute()方法了,
          validateRegist()方法是專門(mén)針對(duì)regist校驗(yàn)的.(格式為validate+方法名)

          posted on 2009-10-13 14:58 super_nini 閱讀(330) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 平南县| 上饶县| 库车县| 鄂托克旗| 和龙市| 瓮安县| 广丰县| 宜兴市| 尉氏县| 阿图什市| 郧西县| 祥云县| 长海县| 枞阳县| 胶南市| 遵义市| 安平县| 宕昌县| 北宁市| 河曲县| 深水埗区| 车险| 吉木乃县| 柳州市| 呼和浩特市| 巍山| 镇雄县| 嫩江县| 五台县| 延津县| 廊坊市| 南丰县| 库车县| 嫩江县| 鹤壁市| 广宁县| 时尚| 阿城市| 锦州市| 崇仁县| 政和县|