?? 入門 14 - forwardaction,includeaction,switchaction - 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=594 -->
<HTML><HEAD><TITLE>入門 14 - ForwardAction,IncludeAction,SwitchAction - 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="入門 14 - ForwardAction,IncludeAction,SwitchAction - 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="入門 14 - ForwardAction,IncludeAction,SwitchAction - 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="入門 14 - ForwardAction,IncludeAction,SwitchAction - 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=594">Log
In</A> </SPAN><A
href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=594&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="入門 14 - ForwardAction,IncludeAction,SwitchAction - 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="入門 14 - ForwardAction,IncludeAction,SwitchAction - 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=594">入門
14 - ForwardAction,IncludeAction,SwitchAction</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="入門 14 - ForwardAction,IncludeAction,SwitchAction - 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>入門 14 -
ForwardAction,IncludeAction,SwitchAction</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 --> 有的時候您只是想要從一個頁面或資源轉換到另一個資源,在MVC/Model
2的架構中,直接使用頁面或資源的路徑來叫用並不是一個好的主意,這會使得控制器沒有機會處理相關的請求事宜。<BR> 您仍應該透過控制器,使用ForwardAction可以幫您完成這個事情,當控制器使用ForwardAction時perform()方法時,它會使用屬性parameter所設定的路徑進行forward的動作,一個設定ForwardAction的例子如下:
<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><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>/></PRE></DIV></DIV><BR> 同樣的,有時候您會需要引入一個頁面或資源,這時您可以使用IncludeAction,其設定方式與ForwardAction相同,只不過它是用include的方式來調用頁面或資源:
<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><action
path=<SPAN class=java-quote>"/SomeResource"</SPAN>
type=<SPAN class=java-quote>"org.apache.struts.actions.IncludeAction"</SPAN>
parameter=<SPAN class=java-quote>"/path/someServlet"</SPAN>/></PRE></DIV></DIV><BR> SwitchAction用於從一個模組轉換至另一個模組,當您的應用程式分作多個模組時,您有兩個方法可以在模組之間切換,第一個方法是使用相對於Context的路徑來進行
forward 查找,我們之前在介紹模組化時就是使用這個方法:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeHeader
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -