📄 入門 11 - 協同開發 - 模組化程式 - 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=512 -->
<HTML><HEAD><TITLE>入門 11 - 協同開發 - 模組化程式 - 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="入門 11 - 協同開發 - 模組化程式 - 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="入門 11 - 協同開發 - 模組化程式 - 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="入門 11 - 協同開發 - 模組化程式 - 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=512">Log
In</A> </SPAN><A
href="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=512&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="入門 11 - 協同開發 - 模組化程式 - 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="入門 11 - 協同開發 - 模組化程式 - 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=512">入門
11 - 協同開發 - 模組化程式</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="入門 11 - 協同開發 - 模組化程式 - 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>入門 11 - 協同開發 -
模組化程式</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 --> Struts中關於ActionForm、Action、ActionMapping等等都是在
struts-config.xml中加以設定,在大型網站的開發中,有很多小團隊會負責不同的模組,如果每一個團隊都要對struts-
config.xml進行設定,將會導致struts-config.xml的版本控制問題。<BR> 在Struts
1.1中,您可以為不同的模組分配不同的struts-config.xml設定檔,方法是在ActionServlet的config參數後加上後綴字,例如我們將使用者登入的工作切分為login模組,則我們可以這麼在web.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>web.xml</B></DIV>
<DIV class=codeContent><PRE><servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/conf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/login</param-name>
<param-value>/WEB-INF/conf/struts-config-login.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet></PRE></DIV></DIV><BR> config/login指定了login模組所使用的設定檔struts-config-login.xml,現在login模組的開發人員只要專心於自己的開發與設定檔,就如同之前的主題一般的設定,當ActionServlet收到請求時,它是根據模組前綴來瞭解該使用哪一個模組,例如:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeContent><PRE>http:<SPAN class=java-comment>//localhost:8080/HelloStruts/login/Login.<SPAN class=java-keyword>do</SPAN></SPAN></PRE></DIV></DIV><BR> 像上面的URL,/HelloStruts/login/Login.do是相對於domain,/login/Login.do是相對於
Context,而Login.do是相對於模組,當ActionServlet接收到上面這樣的絕對路徑,它判斷URI中相對於Context的前綴為
login,於是得知該使用login模組,在每個模組的struts-config-xxx.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-login.xml.java</B></DIV>
<DIV class=codeContent><PRE><action
path=<SPAN class=java-quote>"/LoginAction"</SPAN>
type=<SPAN class=java-quote>"onlyfun.caterpillar.LoginAction"</SPAN>
name=<SPAN class=java-quote>"userForm"</SPAN>>
<forward
name=<SPAN class=java-quote>"greeting"</SPAN>
path=<SPAN class=java-quote>"/greeting.jsp"</SPAN>/></PRE></DIV></DIV><BR> 則LoginAction會自動被加上login前綴,也就是要使用以下的路徑才可以正確的請求:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeContent><PRE>http:<SPAN class=java-comment>//localhost:8080/HelloStruts/login/LoginAction.<SPAN class=java-keyword>do</SPAN></SPAN></PRE></DIV></DIV><BR> 為了更清楚的瞭解,我們實際來看一個例子,首先我們先完成struts-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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -