?? 入門 08 - actionerror與actionmessage - javaworld@tw.htm
字號:
?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0071)http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=481 -->
<HTML><HEAD><TITLE>入門 08 - ActionError與ActionMessage - JAVAWORLD@TW</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8"><!-- CONFL-BEGIN standardHeader -->
<META http-equiv=Pragma content=no-cache>
<META http-equiv=Expires content=-1>
<SCRIPT language=javascript> var contextPath = '/confluence';</SCRIPT>
<LINK
href="入門 08 - ActionError與ActionMessage - JAVAWORLD@TW.files/main-action.css"
type=text/css rel=stylesheet><LINK href="/confluence/images/icons/favicon.ico"
rel="shortcut icon"><LINK href="/confluence/images/icons/favicon.png"
type=image/png rel=icon>
<SCRIPT
src="入門 08 - ActionError與ActionMessage - JAVAWORLD@TW.files/effects.js"></SCRIPT>
<!-- CONF-END standardHeader -->
<META content="MSHTML 6.00.2800.1491" name=GENERATOR></HEAD>
<BODY onload=placeFocus()>
<DIV id=Content>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=logocell width="60%" rowSpan=2>
<DIV class=spacenametitle><A
href="http://www.javaworld.com.tw/confluence/display/opensrc"><IMG
src="入門 08 - ActionError與ActionMessage - JAVAWORLD@TW.files/confluence_logo.gif"
align=bottom border=0></A> <A
href="http://www.javaworld.com.tw/confluence/display/opensrc">OpenSource
Project</A></DIV></TD>
<TD vAlign=top align=right width="40%">
<TABLE cellSpacing=1 cellPadding=2 align=right bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD class=navItem onmouseover="this.className='navItemOver'"
onclick="window.document.location='/confluence/'"
onmouseout="this.className='navItem'" vAlign=center noWrap
align=middle> <A title="Go to the Confluence Dashboard"
accessKey=h onclick="return false"
href="http://www.javaworld.com.tw/confluence/"><U>H</U>OME</A>
</TD>
<TD class=navItem onmouseover="this.className='navItemOver'"
onclick="window.document.location='/confluence/spaces/listspaces.action'"
onmouseout="this.className='navItem'" vAlign=center noWrap
align=middle> <A title="The spaces available for you"
accessKey=s onclick="return false"
href="http://www.javaworld.com.tw/confluence/spaces/listspaces.action"><U>S</U>PACES</A>
</TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD vAlign=bottom align=right><SPAN class=smalltext><A
href="http://www.javaworld.com.tw/confluence/login.action?os_destination=/pages/viewpage.action?pageId=481">Log
In</A> </SPAN><A
href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=481&decorator=printable"><IMG
title="View a printable version of the current page." height=16
alt="View a printable version of the current page." hspace=1
src="入門 08 - ActionError與ActionMessage - JAVAWORLD@TW.files/print_16.gif"
width=16 align=absMiddle vspace=1 border=0></A> <A
href="http://confluence.atlassian.com/display/DOC"><IMG
title="Get help from the Confluence website." height=16
alt="Get help from the Confluence website." hspace=1
src="入門 08 - ActionError與ActionMessage - JAVAWORLD@TW.files/help_16.gif"
width=16 align=absMiddle vspace=1 border=0></A> </TD></TR></TBODY></TABLE><!-- CONF-BEGIN breadcrumbsAndSearch -->
<DIV class=breadcrumbs width="100%">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD> Location: <A
href="http://www.javaworld.com.tw/confluence/dashboard.action">Dashboard</A>
> <A
href="http://www.javaworld.com.tw/confluence/spaces/viewspacesummary.action?key=opensrc">opensrc</A>
> <A
href="http://www.javaworld.com.tw/confluence/pages/listpages.action?key=opensrc">Pages</A>
> <A
href="http://www.javaworld.com.tw/confluence/display/opensrc/Home">Home</A>
> <A
href="http://www.javaworld.com.tw/confluence/display/opensrc/Struts">Struts</A>
> <A
href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=1958">Struts入門</A>
> <A
href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=481">入門
08 - ActionError與ActionMessage</A> </TD>
<FORM name=searchForm action=/confluence/dosearchsite.action
method=get><INPUT type=hidden value=true name=quickSearch>
<TD vAlign=bottom align=right width=200><A style="TEXT-DECORATION: none"
href="http://www.javaworld.com.tw/confluence/searchsite.action"><U>Q</U>uick
<U>Search</U></A>:<INPUT accessKey=q size=10 name=searchQuery.queryString><INPUT type=submit value=Go><BR><IMG height=1
src="入門 08 - ActionError與ActionMessage - JAVAWORLD@TW.files/spacer.gif"
width=200></TD></FORM></TR></TBODY></TABLE></DIV><!-- CONF-END breadcrumbsAndSearch -->
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=pagebody vAlign=top>
<DIV class=pageheader><SPAN class=pagetitle>入門 08 -
ActionError與ActionMessage</SPAN> </DIV>
<SCRIPT language=javascript> function toggle(id) { var oldAttachmentVersionsBlock = document.getElementById('attachment' + id); with (oldAttachmentVersionsBlock.style) { if ( display == "none" ) display = "" else display = "none" } var arrowIcon = document.getElementById('arrow' + id); with (arrowIcon) { if (src != null && (src.indexOf('/confluence/images/icons/arrow_closed_active_16.gif') != -1 || src.indexOf('/confluence/images/icons/arrow_closing_active_16.gif') != -1)) { src = '/confluence/images/icons/arrow_opening_active_16.gif'; } else { src = '/confluence/images/icons/arrow_closing_active_16.gif'; } } } </SCRIPT>
<DIV class=pagesubheading>Last changed on Oct 27, 2004 by <A
href="http://www.javaworld.com.tw/confluence/display/~koji">koji
lin</A>
</DIV><!-- page content --> ActionForm是表單的物件化,有關於表單資料的完整性檢查工作該在其中進行,例如使用者是否填寫了所有的欄位,ActionForm中所有的屬性是否被設定了,您可以重新定義ActionForm的validate()方法來進行這項工作,例如:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeHeader
style="BORDER-BOTTOM-STYLE: solid"><B>UserForm.java</B></DIV>
<DIV class=codeContent><PRE><SPAN class=java-keyword>package</SPAN> onlyfun.caterpillar;
<SPAN class=java-keyword>import</SPAN> javax.servlet.http.*;
<SPAN class=java-keyword>import</SPAN> org.apache.struts.action.*;
<SPAN class=java-keyword>public</SPAN> class UserForm <SPAN class=java-keyword>extends</SPAN> ActionForm {
<SPAN class=java-keyword>protected</SPAN> <SPAN class=java-object>String</SPAN> name;
<SPAN class=java-keyword>protected</SPAN> <SPAN class=java-object>String</SPAN> password;
<SPAN class=java-keyword>public</SPAN> void setName(<SPAN class=java-object>String</SPAN> name) {
<SPAN class=java-keyword>this</SPAN>.name = name;
}
<SPAN class=java-keyword>public</SPAN> void setPassword(<SPAN class=java-object>String</SPAN> password) {
<SPAN class=java-keyword>this</SPAN>.password = password;
}
<SPAN class=java-keyword>public</SPAN> <SPAN class=java-object>String</SPAN> getName() {
<SPAN class=java-keyword>return</SPAN> name;
}
<SPAN class=java-keyword>public</SPAN> <SPAN class=java-object>String</SPAN> getPassword() {
<SPAN class=java-keyword>return</SPAN> password;
}
<SPAN class=java-keyword>public</SPAN> void reset(ActionMapping mapping, HttpServletRequest req) {
name = <SPAN class=java-keyword>null</SPAN>;
password = <SPAN class=java-keyword>null</SPAN>;
}
<SPAN class=java-keyword>public</SPAN> ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = <SPAN class=java-keyword>new</SPAN> ActionErrors();
<SPAN class=java-keyword>if</SPAN>(getName() == <SPAN class=java-keyword>null</SPAN> || getUsername().length() < 1) {
errors.add(<SPAN class=java-quote>"name"</SPAN>,<SPAN class=java-keyword>new</SPAN> ActionError(<SPAN class=java-quote>"error.name.required"</SPAN>));
}
<SPAN class=java-keyword>if</SPAN>(getPassword() == <SPAN class=java-keyword>null</SPAN> || getPassword().length() < 1) {
errors.add(<SPAN class=java-quote>"password"</SPAN>,<SPAN class=java-keyword>new</SPAN> ActionError(<SPAN class=java-quote>"error.password.required"</SPAN>));
}
<SPAN class=java-keyword>return</SPAN> errors;
}
}</PRE></DIV></DIV><BR> 當使用者發送表單,而表單中有欄位沒有填寫時,則請求中會包括參數名稱,但是值為空字串,如果ActionForm具有某些屬性,而表單並沒有發送對應的參數,則不會設定ActionForm中對應的屬性,這些屬性將為null,我們的validate()中主要在檢查這兩個情況。<BR> validate()方法會傳回ActionErrors物件,ActionErrors可以儲存ActionError的訊息,每一個
ActionError會查詢資源檔中的key-value對應,當validate()丟回ActionErrors物件時,
ActionServlet就不會繼續進行接下來的工作,而是導回structs-config.xml所設定的位置,例如:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeHeader
style="BORDER-BOTTOM-STYLE: solid"><B>struts-config.xml</B></DIV>
<DIV class=codeContent><PRE><global-forwards>
<forward
name=<SPAN class=java-quote>"welcome"</SPAN>
path=<SPAN class=java-quote>"/Welcome.<SPAN class=java-keyword>do</SPAN>"</SPAN>/>
</global-forwards>
<form-beans>
<form-bean
name=<SPAN class=java-quote>"userForm"</SPAN>
type=<SPAN class=java-quote>"onlyfun.caterpillar.UserForm"</SPAN>/>
</form-beans>
<action-mappings>
<action
path=<SPAN class=java-quote>"/Welcome"</SPAN>
type=<SPAN class=java-quote>"org.apache.struts.actions.ForwardAction"</SPAN>
parameter=<SPAN class=java-quote>"/pages/Welcome.jsp"</SPAN>/>
<action
path=<SPAN class=java-quote>"/LoginAction"</SPAN>
type=<SPAN class=java-quote>"onlyfun.caterpillar.LoginAction"</SPAN>
name=<SPAN class=java-quote>"userForm"</SPAN>
validate=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>
input=<SPAN class=java-quote>"/pages/Welcome.jsp"</SPAN>>
<forward name=<SPAN class=java-quote>"greeting"</SPAN> path=<SPAN class=java-quote>"/pages/greeting.jsp"</SPAN>/>
</action>
</action-mappings></PRE></DIV></DIV><BR> 為了要能使用validate()方法,<action>中的validate屬性必須設定為true,而input屬性也是必要的,當validate()傳回ActionErrors時,就會forward至input屬性所設定的位置,ActionErrors中的訊息,我們可以使用<html:errors/>標籤來顯示,待會就會看到。<BR> ActionForm中驗證了屬性為null及空字串的可能,這是資料完整性的驗證,接下來我們要驗證資料的正確性,是否符合我們所設定的名稱與密碼,我們改寫前一個主題的LoginAction,看看寫法有何不同:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeHeader
style="BORDER-BOTTOM-STYLE: solid"><B>LoginAction.java</B></DIV>
<DIV class=codeContent><PRE><SPAN class=java-keyword>package</SPAN> onlyfun.caterpillar;
<SPAN class=java-keyword>import</SPAN> javax.servlet.http.*;
<SPAN class=java-keyword>import</SPAN> org.apache.struts.action.*;
<SPAN class=java-keyword>import</SPAN> org.apache.commons.beanutils.*;
<SPAN class=java-keyword>public</SPAN> class LoginAction <SPAN class=java-keyword>extends</SPAN> Action {
<SPAN class=java-keyword>public</SPAN> ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -