在進(jìn)行完簡(jiǎn)單驗(yàn)證后,如果form中的數(shù)據(jù)不合法,程序就會(huì)forward到指定的JSP頁(yè)(一般是包含form的頁(yè)面),并顯示相應(yīng)的錯(cuò)誤信息。如果form中的數(shù)據(jù)完全正確,程序就會(huì)繼續(xù)執(zhí)行。
一、在validate方法中進(jìn)行簡(jiǎn)單驗(yàn)證
在上一篇文章中我們知道,Struts1.x通過(guò)ActionForm的子類來(lái)封裝了客戶端提交的form中的數(shù)據(jù)。而服務(wù)端程序只需要通過(guò)ActionForm的子類的對(duì)象實(shí)例就可以訪問(wèn)form中的數(shù)據(jù),而如果不使用ActionForm類,就必須通過(guò)request對(duì)象來(lái)獲得form中的數(shù)據(jù)。通過(guò)這種封裝機(jī)制可以使代碼更容易理解。然而,ActionForm類不僅可以封裝form中的數(shù)據(jù),還可以通過(guò)ActionForm類的validate方法來(lái)驗(yàn)證form中的數(shù)據(jù)。validate方法的定義如下:
當(dāng)客戶端向服務(wù)端提交form后,Servlet引擎首先通過(guò)ActionForm的子類的對(duì)象實(shí)例裝載form中的數(shù)據(jù),然后再調(diào)用validate方法進(jìn)行驗(yàn)證。validate方法返回了一個(gè)ActionErrors對(duì)象。這個(gè)對(duì)象相當(dāng)于一個(gè)Map,如果ActionErrors中沒(méi)有錯(cuò)誤信息,Servlet引擎就認(rèn)為form中的數(shù)據(jù)是正確的,這時(shí)服務(wù)端程序就會(huì)繼續(xù)執(zhí)行。如果ActionErrors中有錯(cuò)誤信息,程序就會(huì)跳轉(zhuǎn)到指定的錯(cuò)誤頁(yè)面。下面讓我們通過(guò)一個(gè)完整的例子來(lái)演示一下如何通過(guò)validate方法來(lái)驗(yàn)證form中的數(shù)據(jù)。實(shí)現(xiàn)這個(gè)例子需要如下五步:
【第1步】建立JSP頁(yè)面
在這一步將建立一個(gè)叫simpleValidation.jsp的頁(yè)面,這個(gè)JSP頁(yè)面用于采集用戶的輸入信息。在<samples工程目錄>中建立一個(gè)simpleValidation.jsp文件,并編寫(xiě)如下的代碼:
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>注冊(cè)信息(測(cè)試簡(jiǎn)單驗(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)通過(guò)上面的URL訪問(wèn)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)是簡(jiǎn)單驗(yàn)證,因此,simpleValidation動(dòng)作并不需要處理更多的工作。一個(gè)動(dòng)作對(duì)應(yīng)于一個(gè)動(dòng)作類,這個(gè)動(dòng)作類一般是org.apache.struts.action.Action類的子類。simpleValidation動(dòng)作只做如下兩項(xiàng)工作:
1. 設(shè)置驗(yàn)證成功后,在目標(biāo)頁(yè)中顯示的信息字符串(保存在request的屬性中)。
2. 跳轉(zhuǎn)到目標(biāo)頁(yè)。
simpleValidation動(dòng)作對(duì)應(yīng)的動(dòng)作類是SimpleValidationAction,在<samples工程目錄>"src"action目錄中建立一個(gè)SimpleValidationAction.java文件,并輸入如下的代碼:
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)頁(yè)中顯示的信息字符串
return mapping.findForward("simple"); // 跳轉(zhuǎn)到目錄頁(yè)(simple所指的JSP頁(yè))
}
}
【第3步】建立ActionForm類
在這一步我們來(lái)建立一個(gè)用于接收有戶的提交信息的ActionForm類:SimpleValidationForm。這個(gè)類從 org.apache.struts.action.ActionForm類繼承。在<samples工程目錄> "src"actionform目錄中建立一個(gè)SimpleValidationForm.java文件,代碼如下:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class SimpleValidationForm extends ActionForm
{
// 以下四個(gè)變量分別對(duì)應(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;
}
// 開(kāi)始驗(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中沒(méi)有錯(cuò)誤信息,
// 就會(huì)調(diào)用SimpleValidationAction類的對(duì)象實(shí)例來(lái)執(zhí)行execute方法。
return error;
}
}
在編寫(xiě)SimpleValidationAction類時(shí)應(yīng)注意如下八點(diǎn):
1. 要想在ActionForm類中進(jìn)行驗(yàn)證,必須在ActionForm類的子類中覆蓋validate方法。
2. validate方法在ActionForm類的對(duì)象實(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í)例對(duì)象返回錯(cuò)誤信息
4. ActionErrors類的構(gòu)造方法的第二個(gè)參是一個(gè)ActionMessage類的對(duì)象實(shí)例,而不是錯(cuò)誤描述信息。
5.ActionMessage類的構(gòu)造方法的參數(shù)并不是錯(cuò)誤描述信息,而是錯(cuò)誤描述信息的key,具體的信息在Java屬性文件中(將在下一步實(shí)現(xiàn))。
6. 使用ActionForm的屬性可以非常好地驗(yàn)證字符串類型,但對(duì)于其他的數(shù)據(jù)類型(如整型)的某些驗(yàn)證卻不太適合。如當(dāng)用戶提交數(shù)據(jù)時(shí),本該提交一個(gè)整數(shù),但用戶卻提交了一個(gè)非整數(shù)信息。對(duì)于這種情況,在ActionForm類的對(duì)象實(shí)例中這個(gè)用戶提交的數(shù)據(jù)的值為0。雖然使用ActionForm類的屬性無(wú)法準(zhǔn)確驗(yàn)證這種情況,但我們可以使用validate方法的第二個(gè)參數(shù)request的getParameter方法直接獲得客戶端提交的數(shù)據(jù)來(lái)進(jìn)行驗(yàn)證。
7. 如果ActionErrors對(duì)象中有錯(cuò)誤信息,在JSP中需要使用<html:errors>標(biāo)簽顯示錯(cuò)誤信息。
8. Struts實(shí)際上是將ActionErrors對(duì)象以org.apache.struts.action.ERROR作為鍵值保存在了request的屬性中。因此,<html:errors>標(biāo)簽實(shí)際上是從request的屬性中獲得的錯(cuò)誤信息描述。如我們也可以通過(guò)如下的Java代碼來(lái)獲得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));
%>
在編寫(xiě)SimpleValidationAction類時(shí)應(yīng)注意如下八點(diǎn):
1. 要想在ActionForm類中進(jìn)行驗(yàn)證,必須在ActionForm類的子類中覆蓋validate方法。
2. validate方法在ActionForm類的對(duì)象實(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í)例對(duì)象返回錯(cuò)誤信息
4. ActionErrors類的構(gòu)造方法的第二個(gè)參是一個(gè)ActionMessage類的對(duì)象實(shí)例,而不是錯(cuò)誤描述信息。
5.ActionMessage類的構(gòu)造方法的參數(shù)并不是錯(cuò)誤描述信息,而是錯(cuò)誤描述信息的key,具體的信息在Java屬性文件中(將在下一步實(shí)現(xiàn))。
6. 使用ActionForm的屬性可以非常好地驗(yàn)證字符串類型,但對(duì)于其他的數(shù)據(jù)類型(如整型)的某些驗(yàn)證卻不太適合。如當(dāng)用戶提交數(shù)據(jù)時(shí),本該提交一個(gè)整數(shù),但用戶卻提交了一個(gè)非整數(shù)信息。對(duì)于這種情況,在ActionForm類的對(duì)象實(shí)例中這個(gè)用戶提交的數(shù)據(jù)的值為0。雖然使用ActionForm類的屬性無(wú)法準(zhǔn)確驗(yàn)證這種情況,但我們可以使用validate方法的第二個(gè)參數(shù)request的getParameter方法直接獲得客戶端提交的數(shù)據(jù)來(lái)進(jìn)行驗(yàn)證。
7. 如果ActionErrors對(duì)象中有錯(cuò)誤信息,在JSP中需要使用<html:errors>標(biāo)簽顯示錯(cuò)誤信息。
8.
Struts實(shí)際上是將ActionErrors對(duì)象以org.apache.struts.action.ERROR作為鍵值保存在了request的屬性中。因此,<html:errors>標(biāo)簽實(shí)際上是從request的屬性中獲得的錯(cuò)誤信息描述。如我們也可以通過(guò)如下的Java代碼來(lái)獲得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.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)簽用來(lái)定義ActionForm。在<form-beans>標(biāo)簽中加入如下所示的<form-bean>標(biāo)簽:
2. 配置<action>標(biāo)簽
這個(gè)標(biāo)簽用來(lái)定義Struts中的動(dòng)作類。在<action-mappings>標(biāo)簽中加入如下所示的<action>標(biāo)簽:
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)作,必須以“/”開(kāi)頭。
3. scope:表示ActionForm類的對(duì)象實(shí)例(在本例中是SimpleValidationForm類的對(duì)
象實(shí)例)保存的范圍。這個(gè)屬性值只能取request和session。默認(rèn)值是session。如果scope的值為request,表示將SimpleValidationForm類的對(duì)象實(shí)例以simpleValidationForm作為鍵值保存到了request的屬性中。如果scope的值為session,表示不將SimpleValidationForm類的對(duì)象實(shí)例保存到request的屬性中。但不管scope的值是request還是session。Struts都會(huì)將SimpleValidationForm類的對(duì)象實(shí)例保存到session的屬性中。
4. type:表示SimpleValidationAction類的全名。
5. input:表示如果客戶端提交的數(shù)據(jù)未通過(guò)簡(jiǎn)單驗(yàn)證后要跳轉(zhuǎn)到的頁(yè)面,也就是在
SimpleValidationForm類的validate方法中返回的ActionErrors對(duì)象中含有錯(cuò)誤描述信息。Struts會(huì)自動(dòng)跳轉(zhuǎn)到input屬性所指的JSP頁(yè)面。
<action>標(biāo)簽中的子標(biāo)簽<forward>定義了可以在動(dòng)作類(在本例中就是SimpleValidationAction類)中讀取的forward頁(yè)的URL。
2. 配置<message-resources>標(biāo)簽
這個(gè)標(biāo)簽用來(lái)定義程序中所使用的屬性文件。在struts-config.xml文件的<struts-config>根節(jié)點(diǎn)中加入如下內(nèi)容:
其中parameter屬性表示屬性文件的路徑,在本例中屬性文件ErrorDescription.properties位于struts目錄中,因此,parameter屬性的值為struts.ErrorDescription。擴(kuò)展名properties不用寫(xiě)。其中“.”也可以使用“/”或“"”代替。
下面我們測(cè)試一下這個(gè)例子程序。首先啟動(dòng)Tomcat,然后在IE中輸入如下的URL:
http://localhost:8080/samples/simpleValidation.jsp
訪問(wèn)上面的URL后的界面如圖1所示。

圖1

圖2