在 hello.jsp 網頁上,不輸入姓名,直接單擊【 Submit 】 按鈕,會看到如圖 2-6 所示的網頁。
圖 2-6? 表單驗證失敗的 hello.jsp 網頁
當客戶提交 HelloForm 表單時, 請求路徑為 “ /HelloWorld.do ”:
<html:form action="/HelloWorld.do" focus="userName" >
服務器端執行表單驗證流程如下。
( 1 ) Servlet 容器在 web.xml 文件中尋找 <url-pattern> 屬性為“ *.do ”的 <servlet-mapping> 元素:
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
( 2 ) Servlet 容器依據以上 <servlet-mapping> 元素的 <servlet-name> 屬性“ action ”,在 web.xml 文件中尋找匹配的 <servlet> 元素:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>
( 3 ) Servlet 容器把請求轉發給以上 <servlet> 元素指定的 ActionServlet , ActionServlet 依據用戶請求路徑 “ /HelloWorld.do ”, 在 Struts 配置文件中檢索 path 屬性為 “ /HelloWorld ” 的 <action> 元素 :
<action??? path????? = "/HelloWorld"
????????? type????? = "hello.HelloAction"
????????? name????? = "HelloForm"
??? ??????scope???? = "request"
????????? validate? = "true"
? ????????input???? = "/hello.jsp"
?>
??????? <forward name="SayHello" path="/hello.jsp" />
?</action>
?
|
更確切地說, ActionServlet 此時檢索的是 ActionMapping 對象,而不是直接訪問 Struts 配置文件中的 <action> 元素。因為 在 ActionServlet 初始化的時候,會加載 Struts 配置文件,把各種配置信息保存在相應的配置類的實例中,例如 <action> 元素的配置信息存放在 ActionMapping 對象中。 |
( 4 ) ActionServlet 根據 <action> 元素的 name 屬性,創建一個 HelloForm 對象,把客戶提交的表單數據傳給 HelloForm 對象,再把 HelloForm 對象保存在 <action> 元素的 scope 屬性指定的 request 范圍內。
( 5 )由于 <action> 元素的 validate 屬性為 true , ActionServlet 調用 HelloForm 對象的 validate() 方法執行表單驗證:
public ActionErrors validate(ActionMapping mapping,
???????????????????????????????? HttpServletRequest request) {
???????? ?? ActionErrors errors = new ActionErrors();
????????????if ((userName == null) || (userName.length() < 1))
??????? ????errors.add("username", new ActionMessage("hello.no.username.error"));
????????????return errors;
}
( 6 ) HelloForm 對象的 validate() 方法返回一個 ActionErrors 對象,里面包含一個 ActionMessage 對象,這個 ActionMessage 對象中封裝了錯誤消息,消息 key 為“ hello.no.username.error ” , 在 Resource Bundle 中與值匹配的消息文本為:
hello.no.username.error=Please enter a <i>UserName</i> to say hello to!
( 7 ) ActionServlet 把 HelloForm 的 validate() 方法返回的 ActionErrors 對象保存在 request 范圍內,然后根據 <action> 元素的 input 屬性,把客戶請求轉發給 hello.jsp 。
( 8 ) hello.jsp 的 <html:errors> 標簽從 request 范圍內讀取 ActionErrors 對象,再從 ActionErrors 對象中讀取 ActionMessage 對象,把它包含的錯誤消息顯示在網頁上。
二、 邏輯驗證失敗的流程
接下來在 hello.jsp 的 HTML 表單中輸入姓名“ Monster ”,然后單擊【 Submit 】 按鈕。當服務器端響應客戶請求時,驗證流程如下。
( 1 )表單驗證 的流程( 1 )~( 4 )。
( 2 ) ActionServlet 調用 HelloForm 對象的 validate() 方法,這次 validate() 方法返回的 ActionErrors 對象中不包含任何 ActionMessage 對象,表示表單驗證成功。
( 3 ) ActionServlet 查找 HelloAction 實例是否存在,如果不存在就創建一個實例。然后調用 HelloAction 的 execute() 方法。
( 4 ) HelloAction 的 execute() 方法先進行邏輯驗證,由于沒有通過邏輯驗證,就創建一個 ActionMessage 對象,這個 ActionMessage 對象封裝了錯誤消息,消息 key 為“ hello.dont.talk.to.monster ”,在 Resource Bundle 中與值匹配的消息文本為:
hello.dont.talk.to.monster=We don't want to say hello to Monster!!!
execute() 方法把 ActionMessage 對象保存在 ActionMessages 對象中,再把 ActionMessages 對象存放在 request 范圍內。最后返回一個 ActionForward 對象,該對象包含的請求轉發路徑為 <action> 元素的 input 屬性指定的 hello.jsp 。
以下是 execute() 方法中進行邏輯驗證的代碼:
ActionMessages errors = new ActionMessages();
String userName = (String)((HelloForm) form).getUserName();
String badUserName = "Monster";
?
if (userName.equalsIgnoreCase(badUserName)) {
???? errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName ));
???? saveErrors(request, errors);
???? return (new ActionForward(mapping.getInput()));
}
( 5 ) ActionServlet 依據 HelloAction 返回的 ActionForward 對象,再把請求轉發給 hello.jsp 。
( 6 ) hello.jsp 的 <html:errors> 標簽從 request 范圍內讀取 ActionMessages 對象,再從 ActionMessages 對象中讀取 ActionMessage 對象,把它包含的錯誤消息顯示在網頁上, 如圖 所示。
邏輯驗證失敗時的 hello.jsp 網頁
三 、邏輯驗證成功的流程
接下來,在 hello.jsp 的 HTML 表單中輸入姓名“ Weiqin ”,然后單擊【 Submit 】 按鈕。當服務器端響應客戶請求時,流程如下。
( 1 )重復 二 的流程( 1 )~( 3 )。
( 2 ) HelloAction 的 execute() 方法先執行邏輯驗證,這次通過了驗證,然后執行相關的業務邏輯,最后調用 ActionMapping.findForward() 方法,參數為 “ SayHello ”:
// Forward control to the specified success URI
return (mapping.findForward("SayHello"));
( 3 ) ActionMapping.findForward() 方法從 <action> 元素中尋找 name 屬性為 “ SayHello ”的 <forward> 子元素,然后返回與之對應的 ActionForward 對象,它代表的請求轉發路徑為“ /hello.jsp ”。
|
更確切地說, ActionMapping 從本身包含的 HashMap 中查找 name 屬性為 “ SayHello ” 的 ActionForward 對象。在 ActionServlet 初始化時會加載 Struts 配置文件,把 <action> 元素的配置信息存放在 ActionMapping 對象中。 <action> 元素中可以包含多個 <forward> 子元素,每個 <forward> 子元素的配置信息存放在一個 ActionForward 對象中,這些 ActionForward 對象存放在 ActionMapping 對象的 HashMap 中。 |
( 4 ) HelloAction 的 execute() 方法 然后把 ActionForward 對象返回給 ActionServlet , ActionServlet 再把客戶請求轉發給 hello.jsp 。
( 5 ) hello.jsp 的 <bean:message> 標簽從 Resource Bundle 中讀取文本,把它們輸出到網頁上,最后生成 動態網頁,如圖 所示。
? 通過數據驗證的 hello.jsp 網頁