一、表單驗(yàn)證的流程
在
hello.jsp
網(wǎng)頁(yè)上,不輸入姓名,直接單擊【
Submit
】
按鈕,會(huì)看到如圖
2-6
所示的網(wǎng)頁(yè)。
圖
2-6?
表單驗(yàn)證失敗的
hello.jsp
網(wǎng)頁(yè)
當(dāng)客戶提交
HelloForm
表單時(shí),
請(qǐng)求路徑為
“
/HelloWorld.do
”:
<html:form action="/HelloWorld.do" focus="userName" >
服務(wù)器端執(zhí)行表單驗(yàn)證流程如下。
(
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
容器依據(jù)以上
<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
容器把請(qǐng)求轉(zhuǎn)發(fā)給以上
<servlet>
元素指定的
ActionServlet
,
ActionServlet
依據(jù)用戶請(qǐng)求路徑
“
/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>
?
|
更確切地說(shuō),
ActionServlet
此時(shí)檢索的是
ActionMapping
對(duì)象,而不是直接訪問
Struts
配置文件中的
<action>
元素。因?yàn)?/span>
在
ActionServlet
初始化的時(shí)候,會(huì)加載
Struts
配置文件,把各種配置信息保存在相應(yīng)的配置類的實(shí)例中,例如
<action>
元素的配置信息存放在
ActionMapping
對(duì)象中。
|
(
4
)
ActionServlet
根據(jù)
<action>
元素的
name
屬性,創(chuàng)建一個(gè)
HelloForm
對(duì)象,把客戶提交的表單數(shù)據(jù)傳給
HelloForm
對(duì)象,再把
HelloForm
對(duì)象保存在
<action>
元素的
scope
屬性指定的
request
范圍內(nèi)。
(
5
)由于
<action>
元素的
validate
屬性為
true
,
ActionServlet
調(diào)用
HelloForm
對(duì)象的
validate()
方法執(zhí)行表單驗(yàn)證:
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
對(duì)象的
validate()
方法返回一個(gè)
ActionErrors
對(duì)象,里面包含一個(gè)
ActionMessage
對(duì)象,這個(gè)
ActionMessage
對(duì)象中封裝了錯(cuò)誤消息,消息
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
對(duì)象保存在
request
范圍內(nèi),然后根據(jù)
<action>
元素的
input
屬性,把客戶請(qǐng)求轉(zhuǎn)發(fā)給
hello.jsp
。
(
8
)
hello.jsp
的
<html:errors>
標(biāo)簽從
request
范圍內(nèi)讀取
ActionErrors
對(duì)象,再?gòu)?/span>
ActionErrors
對(duì)象中讀取
ActionMessage
對(duì)象,把它包含的錯(cuò)誤消息顯示在網(wǎng)頁(yè)上。
二、
邏輯驗(yàn)證失敗的流程
接下來(lái)在
hello.jsp
的
HTML
表單中輸入姓名“
Monster
”,然后單擊【
Submit
】
按鈕。當(dāng)服務(wù)器端響應(yīng)客戶請(qǐng)求時(shí),驗(yàn)證流程如下。
(
1
)表單驗(yàn)證
的流程(
1
)~(
4
)。
(
2
)
ActionServlet
調(diào)用
HelloForm
對(duì)象的
validate()
方法,這次
validate()
方法返回的
ActionErrors
對(duì)象中不包含任何
ActionMessage
對(duì)象,表示表單驗(yàn)證成功。
(
3
)
ActionServlet
查找
HelloAction
實(shí)例是否存在,如果不存在就創(chuàng)建一個(gè)實(shí)例。然后調(diào)用
HelloAction
的
execute()
方法。
(
4
)
HelloAction
的
execute()
方法先進(jìn)行邏輯驗(yàn)證,由于沒有通過(guò)邏輯驗(yàn)證,就創(chuàng)建一個(gè)
ActionMessage
對(duì)象,這個(gè)
ActionMessage
對(duì)象封裝了錯(cuò)誤消息,消息
key
為“
hello.dont.talk.to.monster
”,在
Resource Bundle
中與值匹配的消息文本為:
hello.dont.talk.to.monster=We don't want to say hello to Monster!!!
execute()
方法把
ActionMessage
對(duì)象保存在
ActionMessages
對(duì)象中,再把
ActionMessages
對(duì)象存放在
request
范圍內(nèi)。最后返回一個(gè)
ActionForward
對(duì)象,該對(duì)象包含的請(qǐng)求轉(zhuǎn)發(fā)路徑為
<action>
元素的
input
屬性指定的
hello.jsp
。
以下是
execute()
方法中進(jìn)行邏輯驗(yàn)證的代碼:
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
依據(jù)
HelloAction
返回的
ActionForward
對(duì)象,再把請(qǐng)求轉(zhuǎn)發(fā)給
hello.jsp
。
(
6
)
hello.jsp
的
<html:errors>
標(biāo)簽從
request
范圍內(nèi)讀取
ActionMessages
對(duì)象,再?gòu)?/span>
ActionMessages
對(duì)象中讀取
ActionMessage
對(duì)象,把它包含的錯(cuò)誤消息顯示在網(wǎng)頁(yè)上,
如圖
所示。
邏輯驗(yàn)證失敗時(shí)的
hello.jsp
網(wǎng)頁(yè)
三
、邏輯驗(yàn)證成功的流程
接下來(lái),在
hello.jsp
的
HTML
表單中輸入姓名“
Weiqin
”,然后單擊【
Submit
】
按鈕。當(dāng)服務(wù)器端響應(yīng)客戶請(qǐng)求時(shí),流程如下。
(
1
)重復(fù)
二
的流程(
1
)~(
3
)。
(
2
)
HelloAction
的
execute()
方法先執(zhí)行邏輯驗(yàn)證,這次通過(guò)了驗(yàn)證,然后執(zhí)行相關(guān)的業(yè)務(wù)邏輯,最后調(diào)用
ActionMapping.findForward()
方法,參數(shù)為
“
SayHello
”:
// Forward control to the specified success URI
return (mapping.findForward("SayHello"));
(
3
)
ActionMapping.findForward()
方法從
<action>
元素中尋找
name
屬性為
“
SayHello
”的
<forward>
子元素,然后返回與之對(duì)應(yīng)的
ActionForward
對(duì)象,它代表的請(qǐng)求轉(zhuǎn)發(fā)路徑為“
/hello.jsp
”。
|
更確切地說(shuō),
ActionMapping
從本身包含的
HashMap
中查找
name
屬性為
“
SayHello
”
的
ActionForward
對(duì)象。在
ActionServlet
初始化時(shí)會(huì)加載
Struts
配置文件,把
<action>
元素的配置信息存放在
ActionMapping
對(duì)象中。
<action>
元素中可以包含多個(gè)
<forward>
子元素,每個(gè)
<forward>
子元素的配置信息存放在一個(gè)
ActionForward
對(duì)象中,這些
ActionForward
對(duì)象存放在
ActionMapping
對(duì)象的
HashMap
中。
|
(
4
)
HelloAction
的
execute()
方法
然后把
ActionForward
對(duì)象返回給
ActionServlet
,
ActionServlet
再把客戶請(qǐng)求轉(zhuǎn)發(fā)給
hello.jsp
。
(
5
)
hello.jsp
的
<bean:message>
標(biāo)簽從
Resource Bundle
中讀取文本,把它們輸出到網(wǎng)頁(yè)上,最后生成
動(dòng)態(tài)網(wǎng)頁(yè),如圖
所示。
?
通過(guò)數(shù)據(jù)驗(yàn)證的
hello.jsp
網(wǎng)頁(yè)
posted on 2006-08-17 20:22
阿成 閱讀(1907)
評(píng)論(0) 編輯 收藏 所屬分類:
Struts