<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 64,  comments - 9,  trackbacks - 0

    一. ActionSupport是個工具類,他實現了Action, Validatable等接口, Validatable提供validate()方法進行數據驗證.Action只要繼承ActionSupport類,重寫validate()方法就可以進行數據驗證

    二. 校驗的流程
         首先,Struts框架對輸入數據進行類型轉換,然后再進行數據校驗,如果類型轉換與數據校驗都沒有錯誤發生, 就進入execute(),否則請求將被轉發到input視圖

    三. 注冊實例
        首先新建RegistAcion.java

    Java代碼 復制代碼
    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("注冊成功");   
    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代碼 復制代碼
    1. <action name="regist" class="com.RegistAction">  
    2.       <result name="success">/welcome.jsp</result>  
    3.       <result name="input">/regist.jsp</result>  
    4.  </action>  

     接著是注冊頁面和注冊成功頁面

    regist.jsp

    Html代碼 復制代碼
    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="注冊"></s:submit>  
    25.                 </td>  
    26.             </tr>  
    27.     </form>  
    28.   </body>  

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

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

    2. 這是一種全局的錯誤提示方式,整個系統中只要是字段類型轉換錯誤都會這樣提示,我們也可以改變這種輸出格式,只要在全局的國際資源文件中重寫xwork.default.invalid.fieldvalue就可以了.

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

    新建messageResource. Properties資源文件并添加數據xwork.default.invalid.fieldvalue={0} failure
    修改之后字段類型轉換錯誤提示為 : {0} failure

    3 所有的類型轉換失敗后,struts2會將基本類型設置為0,對象類型設置為null,這里的age的類型為Integer,當類型轉換失敗age值為null,如果age的類型為int,那么轉換失敗后值為0

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

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

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

    Java代碼 復制代碼
    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("注冊成功-regist");   
    14.         return SUCCESS;   
    15.     }   
    16.        
    17.     public void validateRegist() {   
    18.         if(userName.equals("")){   
    19.             addFieldError("userName""請輸入用戶名-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,當請求時會執行regist(),不會再去執行默認的execute()方法了,
    validateRegist()方法是專門針對regist校驗的.(格式為validate+方法名)

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

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2009年10月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久精品国产亚洲7777| jjizz全部免费看片| 免费A级毛片无码A∨男男| 亚洲AV无码成人精品区狼人影院| 18勿入网站免费永久| 亚洲制服丝袜在线播放| 波多野结衣在线免费视频| 亚洲精品中文字幕乱码| 国产精彩免费视频| 亚洲一线产区二线产区精华| 91在线视频免费播放| 亚洲色大网站WWW永久网站| 夫妻免费无码V看片| 香蕉97碰碰视频免费| 亚洲国产av一区二区三区| 精品国产免费人成网站| 亚洲av午夜福利精品一区 | 国产大陆亚洲精品国产| 又粗又大又长又爽免费视频| 免费精品久久久久久中文字幕| 亚洲人成网站色在线入口| 成人免费区一区二区三区| 久久精品国产亚洲AV嫖农村妇女| 在免费jizzjizz在线播| 亚洲天然素人无码专区| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲视屏在线观看| 在人线av无码免费高潮喷水| 久久综合亚洲色hezyo| 亚洲日本中文字幕天堂网| 久久永久免费人妻精品| 亚洲AV男人的天堂在线观看| 亚洲国产成人VA在线观看| 久久精品视频免费看| 亚洲日韩AV一区二区三区中文| 亚洲国产一成久久精品国产成人综合 | 免费人妻无码不卡中文字幕系| 久久亚洲国产最新网站| 久久久久亚洲av成人无码电影| 日韩免费人妻AV无码专区蜜桃| 亚洲欧美中文日韩视频|