隨筆 - 147  文章 - 71  trackbacks - 0
          <2009年2月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          1234567

          常用鏈接

          留言簿(1)

          隨筆分類(146)

          隨筆檔案(147)

          文章分類(28)

          文章檔案(28)

          喜歡的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          簡單驗(yàn)證從本質(zhì)上說就是在服務(wù)端來驗(yàn)證客戶端提交的form中的數(shù)據(jù)。這種驗(yàn)證只是對form中的數(shù)據(jù)規(guī)則進(jìn)行檢查,如必須輸入用戶ID,價(jià)格不能小于0或是對email格式的驗(yàn)證。在這個(gè)驗(yàn)證過程中,并不需要訪問數(shù)據(jù)庫。因此,簡單驗(yàn)證需要在用戶提交form后,并且在服務(wù)器處理form中的數(shù)據(jù)之前進(jìn)行。

              在進(jìn)行完簡單驗(yàn)證后,如果form中的數(shù)據(jù)不合法,程序就會(huì)forward到指定的JSP頁(一般是包含form的頁面),并顯示相應(yīng)的錯(cuò)誤信息。如果form中的數(shù)據(jù)完全正確,程序就會(huì)繼續(xù)執(zhí)行。

          一、在validate方法中進(jìn)行簡單驗(yàn)證


             
          在上一篇文章中我們知道,Struts1.x通過ActionForm的子類來封裝了客戶端提交的form中的數(shù)據(jù)。而服務(wù)端程序只需要通過ActionForm的子類的對象實(shí)例就可以訪問form中的數(shù)據(jù),而如果不使用ActionForm類,就必須通過request對象來獲得form中的數(shù)據(jù)。通過這種封裝機(jī)制可以使代碼更容易理解。然而,ActionForm類不僅可以封裝form中的數(shù)據(jù),還可以通過ActionForm類的validate方法來驗(yàn)證form中的數(shù)據(jù)。validate方法的定義如下:

          public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)

              當(dāng)客戶端向服務(wù)端提交form后,Servlet引擎首先通過ActionForm的子類的對象實(shí)例裝載form中的數(shù)據(jù),然后再調(diào)用validate方法進(jìn)行驗(yàn)證。validate方法返回了一個(gè)ActionErrors對象。這個(gè)對象相當(dāng)于一個(gè)Map,如果ActionErrors中沒有錯(cuò)誤信息,Servlet引擎就認(rèn)為form中的數(shù)據(jù)是正確的,這時(shí)服務(wù)端程序就會(huì)繼續(xù)執(zhí)行。如果ActionErrors中有錯(cuò)誤信息,程序就會(huì)跳轉(zhuǎn)到指定的錯(cuò)誤頁面。下面讓我們通過一個(gè)完整的例子來演示一下如何通過validate方法來驗(yàn)證form中的數(shù)據(jù)。實(shí)現(xiàn)這個(gè)例子需要如下五步:

          【第1步】建立JSP頁面

              在這一步將建立一個(gè)叫simpleValidation.jsp的頁面,這個(gè)JSP頁面用于采集用戶的輸入信息。在<samples工程目錄>中建立一個(gè)simpleValidation.jsp文件,并編寫如下的代碼:

            <%@ page pageEncoding="GBK"%>
            
          <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
            
          <html>
                
          <head>
                    
          <title>注冊信息(測試簡單驗(yàn)證)</title>
                    
          <style type="text/css">
            .text 
          {
                height
          : 20px;
                width
          : 160px;
            
          }
            
          </style>
                
          </head>
                
          <body>
                    
          <html:form action="simpleValidation">
                        
          <table width="100%">
                            
          <tr>
                                
          <td align="right" width="45%"> 用戶名:</td>
                                
          <td width="55%">
                                    
          <html:text property="user" styleClass="text" />
                                    
          <font color="red"><html:errors property="errorUser" /></font>
                                
          </td>
                            
          </tr><tr /><tr />
                            
          <tr>
                                
          <td align="right">登錄密碼:</td>
                                
          <td>
                                    
          <html:password property="password" styleClass="text" />
                                    
          <font color="red"><html:errors property="errorPassword" /></font>
                                
          </td>
                            
          </tr><tr /><tr />
                            
          <tr>
                                
          <td align="right">重復(fù)登錄密碼:</td>
                                
          <td>
                                    
          <html:password property="password1" styleClass="text" />
                                    
          <font color="red"><html:errors property="errorPassword1" /></font>
                                
          </td>
                            
          </tr><tr /><tr />
                            
          <tr>
                                
          <td align="right">電子郵件:</td>
                                
          <td>
                                    
          <html:text property="email" styleClass="text" />
                                    
          <font color="red"><html:errors property="errorEmail" /></font>
                                
          </td>
                            
          </tr><tr /><tr />
                            
          <tr>
                                
          <td align="right"> <br> ${requestScope.success } </td>
                                
          <td align="left"> <br> <html:submit value=" 提交 " /> </td>
                            
          </tr>
                        
          </table>
                    
          </html:form>
                
          </body>
            
          </html>

           

           在啟動(dòng)Tomcat后,在IE的地址欄中輸入如下的URL

          http://localhost:8080/samples/simpleValidation.jsp

          當(dāng)通過上面的URL訪問simpleValidation.jsp時(shí),并不能正確顯示用戶信息采集界面。原因是<html:form>標(biāo)簽使用了一個(gè)simpleValidation,當(dāng)JSP轉(zhuǎn)換成Servlet時(shí),這個(gè)動(dòng)作必須在struts-config.xml文件中正確定義,否則將拋出一個(gè)javax.servlet.jsp.JspException異常。

          【第2步】建立simpleValidation動(dòng)作

          由于本例的著重點(diǎn)是簡單驗(yàn)證,因此,simpleValidation動(dòng)作并不需要處理更多的工作。一個(gè)動(dòng)作對應(yīng)于一個(gè)動(dòng)作類,這個(gè)動(dòng)作類一般是org.apache.struts.action.Action類的子類。simpleValidation動(dòng)作只做如下兩項(xiàng)工作:

          1.  設(shè)置驗(yàn)證成功后,在目標(biāo)頁中顯示的信息字符串(保存在request的屬性中)。

          2.       跳轉(zhuǎn)到目標(biāo)頁。

              simpleValidation動(dòng)作對應(yīng)的動(dòng)作類是SimpleValidationAction,在<samples工程目錄>"src"action目錄中建立一個(gè)SimpleValidationAction.java文件,并輸入如下的代碼:

            package action;
            
            
          import javax.servlet.http.*;
            
          import org.apache.struts.action.*;
            
            
          public class SimpleValidationAction extends Action
            {
                
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                        
          throws Exception
                {        
                    request.setAttribute(
          "success""提交成功!");  // 設(shè)置在目標(biāo)頁中顯示的信息字符串
                    return mapping.findForward("simple");  // 跳轉(zhuǎn)到目錄頁(simple所指的JSP頁)
                }
            }

          【第3步】建立ActionForm類
              在這一步我們來建立一個(gè)用于接收有戶的提交信息的ActionForm類:SimpleValidationForm。這個(gè)類從 org.apache.struts.action.ActionForm類繼承。在<samples工程目錄> "src"actionform目錄中建立一個(gè)SimpleValidationForm.java文件,代碼如下:

            package actionform;
            
            
          import javax.servlet.http.HttpServletRequest;
            
          import org.apache.struts.action.*;

            
          public class SimpleValidationForm extends ActionForm
            {
                
          // 以下四個(gè)變量分別對應(yīng)于simpleValidation.jsp中的四個(gè)文本框中的值。
                private String user;    
                
          private String password;
                
          private String password1;
                
          private String email;
            
                
          public String getUser()  
                {
                    
          return user;
                }
                
          public void setUser(String user)
                {
                    
          this.user = user;
                }
                
          public String getPassword()
                {
                    
          return password;
                }
                
          public void setPassword(String password)
                {
                    
          this.password = password;
                }
                
          public String getPassword1()
                {
                    
          return password1;
                }  
                
          public void setPassword1(String password1)
                {
                    
          this.password1 = password1;
                }
                
          public String getEmail()
                {
                    
          return email;
                }
                
          public void setEmail(String email)
                {
                    
          this.email = email;
                }  
                
          // 開始驗(yàn)證用戶提交的信息 
                public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
                {
                    ActionErrors error 
          = new ActionErrors();
                    
          if (user.equals(""))   // 必須輸入用戶名
                        error.add("errorUser"new ActionMessage("error.user.blank"));
                    
          if (password.equals(""))  // 必須輸入密碼
                        error.add("errorPassword"new ActionMessage("error.password.blank"));
                    
          else if (!password.equals(password1))  // 兩個(gè)登錄密碼必須一致
                        error.add("errorPassword1"new ActionMessage("error.password1.confirmation"));
                    
          if (email.equals(""))  // 必須輸入email
                        error.add("errorEmail"new ActionMessage("error.email.blank"));
                    
          else if (!email.matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+"))  // 驗(yàn)證email的格式是否正確
                        error.add("errorEmail"new ActionMessage("error.email.invalid"));
            
                     // 返回錯(cuò)誤信息,如果error中沒有錯(cuò)誤信息,
                     // 就會(huì)調(diào)用SimpleValidationAction類的對象實(shí)例來執(zhí)行execute方法。
                    return error;        
                }
            }

           

          在編寫SimpleValidationAction類時(shí)應(yīng)注意如下八點(diǎn):
              1. 
          要想在ActionForm類中進(jìn)行驗(yàn)證,必須在ActionForm類的子類中覆蓋validate方法。
              2. validate方法在ActionForm類的對象實(shí)例裝載完用戶提交的數(shù)據(jù)后調(diào)用,因此,在調(diào)用validate方法時(shí),ActionForm類的屬性值已經(jīng)是用戶提交的信息了。所以可以直接使用這些屬性值進(jìn)行驗(yàn)證。
              3. validate方法中驗(yàn)證用戶提交的數(shù)據(jù)時(shí),要使用ActionErrors類的實(shí)例對象返回錯(cuò)誤信息
             
          4. ActionErrors類的構(gòu)造方法的第二個(gè)參是一個(gè)ActionMessage類的對象實(shí)例,而不是錯(cuò)誤描述信息。
              5.ActionMessage類的構(gòu)造方法的參數(shù)并不是錯(cuò)誤描述信息,而是錯(cuò)誤描述信息的key,具體的信息在Java屬性文件中(將在下一步實(shí)現(xiàn))。

              6. 使用ActionForm的屬性可以非常好地驗(yàn)證字符串類型,但對于其他的數(shù)據(jù)類型(如整型)的某些驗(yàn)證卻不太適合。如當(dāng)用戶提交數(shù)據(jù)時(shí),本該提交一個(gè)整數(shù),但用戶卻提交了一個(gè)非整數(shù)信息。對于這種情況,在ActionForm類的對象實(shí)例中這個(gè)用戶提交的數(shù)據(jù)的值為0。雖然使用ActionForm類的屬性無法準(zhǔn)確驗(yàn)證這種情況,但我們可以使用validate方法的第二個(gè)參數(shù)requestgetParameter方法直接獲得客戶端提交的數(shù)據(jù)來進(jìn)行驗(yàn)證。
              7. 如果ActionErrors對象中有錯(cuò)誤信息,在JSP中需要使用<html:errors>標(biāo)簽顯示錯(cuò)誤信息。
              8. Struts實(shí)際上是將ActionErrors對象以org.apache.struts.action.ERROR作為鍵值保存在了request的屬性中。因此,<html:errors>標(biāo)簽實(shí)際上是從request的屬性中獲得的錯(cuò)誤信息描述。如我們也可以通過如下的Java代碼來獲得produceID屬性的錯(cuò)誤描述信息:

            <
              java
          .util.Iterator<org.apache.struts.action.ActionMessage> it =
           ((org.apache.struts.action.ActionErrors)request
                                .getAttribute("org.apache.struts.action.ERROR")).get("productID");
              out.println(((org.apache.struts.util.PropertyMessageResources )request
                 .getAttribute("org.apache.struts.action.MESSAGE")).getMessage("error.productID.blank",null));
            %>

          在編寫SimpleValidationAction類時(shí)應(yīng)注意如下八點(diǎn):
              1. 
          要想在ActionForm類中進(jìn)行驗(yàn)證,必須在ActionForm類的子類中覆蓋validate方法。
              2. validate方法在ActionForm類的對象實(shí)例裝載完用戶提交的數(shù)據(jù)后調(diào)用,因此,在調(diào)用validate方法時(shí),ActionForm類的屬性值已經(jīng)是用戶提交的信息了。所以可以直接使用這些屬性值進(jìn)行驗(yàn)證。
              3. validate方法中驗(yàn)證用戶提交的數(shù)據(jù)時(shí),要使用ActionErrors類的實(shí)例對象返回錯(cuò)誤信息
            
          4. ActionErrors類的構(gòu)造方法的第二個(gè)參是一個(gè)ActionMessage類的對象實(shí)例,而不是錯(cuò)誤描述信息。
             5.ActionMessage類的構(gòu)造方法的參數(shù)并不是錯(cuò)誤描述信息,而是錯(cuò)誤描述信息的key,具體的信息在Java屬性文件中(將在下一步實(shí)現(xiàn))。

              6. 使用ActionForm的屬性可以非常好地驗(yàn)證字符串類型,但對于其他的數(shù)據(jù)類型(如整型)的某些驗(yàn)證卻不太適合。如當(dāng)用戶提交數(shù)據(jù)時(shí),本該提交一個(gè)整數(shù),但用戶卻提交了一個(gè)非整數(shù)信息。對于這種情況,在ActionForm類的對象實(shí)例中這個(gè)用戶提交的數(shù)據(jù)的值為0。雖然使用ActionForm類的屬性無法準(zhǔn)確驗(yàn)證這種情況,但我們可以使用validate方法的第二個(gè)參數(shù)requestgetParameter方法直接獲得客戶端提交的數(shù)據(jù)來進(jìn)行驗(yàn)證。
              7. 如果ActionErrors對象中有錯(cuò)誤信息,在JSP中需要使用<html:errors>標(biāo)簽顯示錯(cuò)誤信息。
             8. Struts實(shí)際上是將ActionErrors對象以org.apache.struts.action.ERROR作為鍵值保存在了request的屬性中。因此,<html:errors>標(biāo)簽實(shí)際上是從request的屬性中獲得的錯(cuò)誤信息描述。如我們也可以通過如下的Java代碼來獲得produceID屬性的錯(cuò)誤描述信息:

            <
              java
          .util.Iterator<org.apache.struts.action.ActionMessage> it =
           ((org.apache.struts.action.ActionErrors)request
                                .getAttribute("org.apache.struts.action.ERROR")).get("productID");
              out.println(((org.apache.struts.util.PropertyMessageResources )request
                 .getAttribute("org.apache.struts.action.MESSAGE")).getMessage("error.productID.blank",null));
            %>

          【第4步】建立Java屬性文件

          Java屬性文件相當(dāng)于資源文件,以key = value形式保存了在程序中需要的字符串信息。Java屬性文件的擴(kuò)展名為properties。在<samples工程目錄>"src目錄中建立一個(gè)struts目錄,在struts目錄中建立一個(gè)ErrorDescription.properties文件,并輸入如下的內(nèi)容:
           
          ErrorDescription.properties

            error.user.blank = User can't be null.
            error.password.blank 
          = Password can't be null.
            error.password1.confirmation 
          = Password doesn't match confirmation.
            error.email.blank 
          = Email can't be null.
            error.email.invalid 
          = It is not a valid email address.

          【第5步】配置struts-config.xml文件

             
          在本例中需要配置struts-config.xml文件的三個(gè)標(biāo)簽:<form-bean><action><message-resources>
              1. 
          配置<form-bean>標(biāo)簽

          這個(gè)標(biāo)簽用來定義ActionForm。在<form-beans>標(biāo)簽中加入如下所示的<form-bean>標(biāo)簽:

          <form-bean name="simpleValidationForm" type="actionform.SimpleValidationForm" />

          2. 配置<action>標(biāo)簽
             
              這個(gè)標(biāo)簽用來定義Struts中的動(dòng)作類。在<action-mappings>標(biāo)簽中加入如下所示的<action>標(biāo)簽:

            <action name="simpleValidationForm" path="/simpleValidation" scope="request" type="action.SimpleValidationAction"
                         input
          ="simpleValidation.jsp">
                <forward name="simple" path="simpleValidation.jsp" />
            </action>

              <action>標(biāo)簽中的屬性含義描述如下:

          1.   name:表示ActionForm的名稱。也就是<form-bean>標(biāo)簽中的name屬性的值。

          2. path:表示Struts動(dòng)作,必須以“/”開頭。

          3. scope:表示ActionForm類的對象實(shí)例(在本例中是SimpleValidationForm類的對

          象實(shí)例)保存的范圍。這個(gè)屬性值只能取requestsession。默認(rèn)值是session。如果scope的值為request,表示將SimpleValidationForm類的對象實(shí)例以simpleValidationForm作為鍵值保存到了request的屬性中。如果scope的值為session,表示不將SimpleValidationForm類的對象實(shí)例保存到request的屬性中。但不管scope的值是request還是sessionStruts都會(huì)將SimpleValidationForm類的對象實(shí)例保存到session的屬性中。

          4. type:表示SimpleValidationAction類的全名。

          5. input:表示如果客戶端提交的數(shù)據(jù)未通過簡單驗(yàn)證后要跳轉(zhuǎn)到的頁面,也就是在

          SimpleValidationForm類的validate方法中返回的ActionErrors對象中含有錯(cuò)誤描述信息。Struts會(huì)自動(dòng)跳轉(zhuǎn)到input屬性所指的JSP頁面。

              <action>標(biāo)簽中的子標(biāo)簽<forward>定義了可以在動(dòng)作類(在本例中就是SimpleValidationAction類)中讀取的forward頁的URL

          2.       配置<message-resources>標(biāo)簽

          這個(gè)標(biāo)簽用來定義程序中所使用的屬性文件。在struts-config.xml文件的<struts-config>根節(jié)點(diǎn)中加入如下內(nèi)容:

          <message-resources parameter="struts.ErrorDescription" />

              其中parameter屬性表示屬性文件的路徑,在本例中屬性文件ErrorDescription.properties位于struts目錄中,因此,parameter屬性的值為struts.ErrorDescription。擴(kuò)展名properties不用寫。其中“.”也可以使用“/”或“"”代替。

             
          下面我們測試一下這個(gè)例子程序。首先啟動(dòng)Tomcat,然后在IE中輸入如下的URL

          http://localhost:8080/samples/simpleValidation.jsp

              訪問上面的URL后的界面如圖1所示。



          圖1
           在不輸入任何信息的情況下,點(diǎn)擊“確定”按鈕后的界面如圖2所示。



          圖2
          posted on 2009-02-23 16:09 飛翔天使 閱讀(194) 評論(0)  編輯  收藏 所屬分類: Struts
          主站蜘蛛池模板: 垫江县| 湘乡市| 湟中县| 金塔县| 武清区| 桂阳县| 海门市| 剑阁县| 呼图壁县| 醴陵市| 屏南县| 平湖市| 子洲县| 肇州县| 崇阳县| 华安县| 梁山县| 鄂托克前旗| 南投市| 江阴市| 彭泽县| 永昌县| 黔江区| 巴青县| 莒南县| 余姚市| 长治县| 古浪县| 扶沟县| 吉安县| 宣汉县| 鄂尔多斯市| 和平区| 沂南县| 龙里县| 青河县| 绥中县| 阳高县| 余姚市| 新安县| 北安市|