使用 Struts2 校驗器校驗用戶注冊信息


基於驗證框架的輸入校驗

一、創建一個struts2項目testValidators。Struts2 初體驗:http://www.cnblogs.com/likailan/p/3280820.html

1.jsp頁面:

注冊頁,register.jsp:

使用<%@ taglib uri="/struts-tags" prefix="s"%>加載struts2標簽庫。

使用<s:fielderror/>標簽輸出相應的字段錯誤信息,fieldName屬性為出錯字段的名字。

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!-- 加載struts2標簽庫 -->
 4 <%@ taglib uri="/struts-tags" prefix="s"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <s:head/>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>注冊</title>
11 </head>
12 <body>
13     <form action="register.action" method="post">
14         <table>
15             <tr>
16                 <td>郵箱:</td>
17                 <td><input type="text" name="email"/></td>
18                 <!-- 使用<s:fielderror/>標簽輸入相應的字段錯誤信息 -->
19                 <td><s:fielderror fieldName="email"/></td>
20             </tr>
21             <tr>
22                 <td>密碼:</td>
23                 <td><input type="password" name="password"/></td>
24                 <td><s:fielderror fieldName="password"/></td>
25             </tr>
26             <tr>
27                 <td>重復密碼:</td><td>
28                 <input type="password" name="repassword"/></td>
29                 <td><s:fielderror fieldName="repassword"/></td>
30             </tr>
31             <tr>
32                 <td>電話:</td>
33                 <td><input type="text" name="phone"/></td>
34                 <td><s:fielderror fieldName="phone"/></td>
35             </tr>
36             <tr>
37                 <td><input type="submit" value="提交"/></td>
38                 <td></td>
39                 <td></td>
40             </tr>
41         </table>
42     </form>
43 </body>
44 </html>

 

注冊成功頁,success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注冊成功</title>
</head>
<body>
    歡迎你 ${email }
</body>
</html

 

2.RegisterAction類。

需要校驗的字段必需在類中提供相應的字段和相應的get,set方法。

 1 import com.opensymphony.xwork2.ActionSupport;
 2 
 3 public class RegisterAction extends ActionSupport{
 4     //需要校驗的字段必需在類中提供相應的字段和相應的get,set方法
 5     private String email;
 6     private String password;
 7     private String repassword;
 8     private String phone;
 9     
10     public String execute() throws Exception {
11         return SUCCESS;
12     }
13 
14     public String getEmail() {
15         return email;
16     }
17 
18     public void setEmail(String email) {
19         this.email = email;
20     }
21 
22     public String getPassword() {
23         return password;
24     }
25 
26     public void setPassword(String password) {
27         this.password = password;
28     }
29 
30     public String getRepassword() {
31         return repassword;
32     }
33 
34     public void setRepassword(String repassword) {
35         this.repassword = repassword;
36     }
37 
38     public String getPhone() {
39         return phone;
40     }
41 
42     public void setPhone(String phone) {
43         this.phone = phone;
44     }
45 
46 }

 

3.strruts2配置文件 ,struts.xml:

添加name為input的result標簽,校驗失敗時struts2會自動把頁面轉到名為"input"的頁面中

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.i18n.encoding" value="UTF-8" />
    
    <package name="default" namespace="/" extends="struts-default">
        <action name="register" class="RegisterAction">
            <result name="success">success.jsp</result>
            <!-- 校驗失敗后,struts2會把頁面轉到名為"input"的頁面中 -->
            <result name="input">register.jsp</result>
        </action>
    </package>

</struts>

 

二、編寫校驗器配置文件

1. 命名規則:action類名-validation.xml.
2. 一個action對應多個邏輯處理方法:指定校驗某個特定方法的方式: action類名-name屬性值-validation.xml.(name屬性名:在strtus配置文件中對應的 action 標簽的 name 屬性值)
3. 配置文件存放位置:放在與Action相同的文件夾內。
4. 驗證規則:先加載action類名-validation.xml,然后加載action類名-name屬性名-validation.xml文件。

校驗器配置文件,RegisterAction-register-validation.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE validators PUBLIC
 3         "-//Apache Struts//XWork Validator 1.0.2//EN"
 4         "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
 5 
 6 <validators>
 7     <!-- name值為需要被校驗的字段,這里對email字段進行校驗 -->
 8     <field name="email">
 9         <!-- type值為校驗器名,requiredstring:非空字符串校驗器 -->
10         <field-validator type="requiredstring">
11             <message>郵箱不能為空</message>
12         </field-validator>
13         <!-- 一個字段可以配置多個校驗器,email:校驗郵箱格式 -->
14         <field-validator type="email">
15             <message>郵箱格式不正確</message>
16         </field-validator>
17     </field>
18     
19     <field name="password">
20         <field-validator type="requiredstring">
21             <message>密碼不能為空</message>
22         </field-validator>
23         <!-- stringlength:字符串長度校驗 -->
24         <field-validator type="stringlength">
25             <!-- 為校驗器指定參數實現不同的校驗規則,minlength:最少長度 -->
26             <param name="minLength">6</param>
27             <message>密碼長度不能少於6位</message>
28         </field-validator>
29     </field>
30     
31     <!-- 校驗兩次密碼是否一致,使用 fieldexpression 校驗器。expression參數指定判斷用的表達式-->
32     <field name="repassword">
33         <field-validator type="fieldexpression">
34             <!-- 表達式要寫在   <![CDATA[]]>   內 -->
35             <param name="expression"><![CDATA[repassword==password]]></param>
36             <message>兩次密碼不一至</message>
37         </field-validator>
38     </field>
39     
40     <!-- 校驗電話格式,使用regex正則表達示校驗,regexExpression參數指定正則表達式 -->
41     <field name="phone">
42         <field-validator type="regex">
43             <!-- 注意!2.3.15前的版本使用 expression 參數 -->
44             <param name="regexExpression"><![CDATA[\d{11}]]></param>
45             <message>電話號碼格式不正確</message>
46         </field-validator>
47     </field>
48     
49 </validators>

上面寫法為字段校驗風格:先指定校驗的屬性:我來校驗誰,由誰來校驗!

下面是非字段校驗風格:先指定校驗器:由誰來校驗,來校驗誰!

<validator type="校驗器名">
    <param name="fieldName">需要被校驗的字段</param>
    <!--此處需要為不同校驗器指定數量不等的校驗規則 -->
    <param name="參數名">參數值</param>
    <!--校驗失敗后的提示信息 -->
    <message>校驗失敗后的提示信息</message>
</validator>

struts2提供了大量的內置校驗器:你可以在xwork-core-2.1.6.jar的com.opensymphony.xwork2.validator.validators下找到如下配置文件:default.xml。里面列出了所有的內置校驗器。

required:必填校驗器,要求field的值不能為null。

requiredstring:必填字符串校驗器,要求field的值不能為null,並且長度大於0

  屬性——trim:指定在校驗之前是否去除字段串前后的空格。

stringlength:字段長度校驗器,要求fidle的值必須在指定的范圍內,否則校驗失敗。

  屬性——minLength:指定最小長度。   maxLength:指定量大長度。   trim:指定在校驗之前是否去除字段串前后的空格。 

regex:正則表達式校驗器,檢查衩校驗的field是否匹配一個正則表達式。

  屬性——expression:指定正則表達式(2.3.15版用regexExpression)。     caseSensitive:指定進行正則表達式匹配時是否區分大小寫。

int:整數校驗器,要求field的整數值 必須在指定范圍內。

  屬性——min:指定最小值。   max指定最大值。

double:雙精度浮點數校驗器,要求field的雙精度浮點數值必須在指定范圍內。

  屬性——min:指定最小值。   max指定最大值。

fieldexpression:字段OGNL表達式校驗器。要求field滿足一個ognl表達式。

  屬性——expression:指定ognl表達式(表達式要寫在 <![CDATA[]]> 內)。

email:郵件地址校驗器。要求如果field的值非空,則必需是合法的郵件地址。

url:網址校驗器。要求如果field的值非空,則必需是合法的 url 地址。

date:日期校驗器,要求field的日期值必須在指定范圍內

  屬性——min:指定最小值。   max指定最大值。

conversion:轉換校驗器,指定在類型轉換失敗時,提示錯誤信息。

visitor:用於校驗action中的復合屬性。它拽定一個校驗文件用於校驗復合屬性中的屬性。

expression:OGNL表達式校驗器。要求field滿足一個ognl表達式。該邏輯表達式基於ValueStack進行求值。該校驗器不可用在字段校驗器風格的配置中!

  屬性——expression:指定ognl表達式(表達式要寫在 <![CDATA[]]> 內)。

 

三、現在Visitor校驗器就起作用了,查看結果:


頁面:

輸入錯誤的信息,校驗不通過。跳轉到名用input的視圖里,即register,jsp:

輸入正確的信息,校驗通過:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM