📄 入門 11 - 協同開發 - 模組化程式 - javaworld@tw.htm
字号:
<DIV class=codeContent><PRE><struts-config>
<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>
<global-forwards>
<forward
name=<SPAN class=java-quote>"login"</SPAN>
contextRelative=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>
path=<SPAN class=java-quote>"/login/Login.<SPAN class=java-keyword>do</SPAN>"</SPAN>
redirect=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>/>
</global-forwards>
<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-mappings>
<message-resources parameter=<SPAN class=java-quote>"resources.application"</SPAN>/>
</struts-config></PRE></DIV></DIV><BR> 在<global-forward>的login
forward,其路徑指向login模組,這個路徑是相對於Context的,當ActionServlet進行重新導向時,可以由路徑得知該使用login模組。<BR> Welcome.jsp位於/pages目錄下,內容如下:
<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>Welcome.jsp</B></DIV>
<DIV class=codeContent><PRE><%@ taglib uri=<SPAN class=java-quote>"/tags/struts-bean"</SPAN> prefix=<SPAN class=java-quote>"bean"</SPAN> %>
<%@ taglib uri=<SPAN class=java-quote>"/tags/struts-html"</SPAN> prefix=<SPAN class=java-quote>"html"</SPAN> %>
<%@page contentType=<SPAN class=java-quote>"text/html; charset=Big5"</SPAN>%>
<html:html locale=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>>
<head>
<title><bean:message key=<SPAN class=java-quote>"welcome.title"</SPAN>/></title>
<html:base/>
</head>
<body bgcolor=<SPAN class=java-quote>"white"</SPAN>>
歡迎!<html:link forward=<SPAN class=java-quote>"login"</SPAN>>請先登入</html:link>
</body>
</html:html></PRE></DIV></DIV><BR> 在<html:link>的設定中,將會查找struts-config.xml的login
forward,執行結果將會是:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeContent><PRE><html lang=<SPAN class=java-quote>"zh"</SPAN>>
<head>
<title>哈囉!Struts!</title>
<base href=<SPAN class=java-quote>"http:<SPAN class=java-comment>//localhost:8080/HelloStruts/pages/Welcome.jsp"</SPAN>></SPAN>
</head>
<body bgcolor=<SPAN class=java-quote>"white"</SPAN>>
歡迎!<a href=<SPAN class=java-quote>"/HelloStruts/login/Login.<SPAN class=java-keyword>do</SPAN>"</SPAN>>請先登入</a>
</body>
</html></PRE></DIV></DIV><BR> 接下來看看struts-config-login.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</B></DIV>
<DIV class=codeContent><PRE><struts-config>
<form-beans>
<form-bean
name=<SPAN class=java-quote>"userForm"</SPAN>
type=<SPAN class=java-quote>"onlyfun.caterpillar.UserForm"</SPAN>/>
</form-beans>
<global-forwards>
<forward
name=<SPAN class=java-quote>"login"</SPAN>
path=<SPAN class=java-quote>"/Login.<SPAN class=java-keyword>do</SPAN>"</SPAN>/>
</global-forwards>
<action-mappings>
<action
path=<SPAN class=java-quote>"/Login"</SPAN>
type=<SPAN class=java-quote>"org.apache.struts.actions.ForwardAction"</SPAN>
parameter=<SPAN class=java-quote>"/login/login.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>>
<forward
name=<SPAN class=java-quote>"greeting"</SPAN>
path=<SPAN class=java-quote>"/greeting.jsp"</SPAN>/>
</action>
</action-mappings>
<message-resources parameter=<SPAN class=java-quote>"resources.application"</SPAN>/>
</struts-config></PRE></DIV></DIV><BR> ForwardAction的parameter接收相對於Context的路徑設定,除此之外,所有的設定都是相對於login模組的,為了要請求每一個設定的Action,您都必須加上模組前綴,其中我們的login.jsp位於/login目錄下:
<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>login.jsp</B></DIV>
<DIV class=codeContent><PRE><%@ taglib uri=<SPAN class=java-quote>"/tags/struts-bean"</SPAN> prefix=<SPAN class=java-quote>"bean"</SPAN> %>
<%@ taglib uri=<SPAN class=java-quote>"/tags/struts-html"</SPAN> prefix=<SPAN class=java-quote>"html"</SPAN> %>
<%@page contentType=<SPAN class=java-quote>"text/html; charset=Big5"</SPAN>%>
<html:html locale=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>>
<head>
<title><bean:message key=<SPAN class=java-quote>"welcome.title"</SPAN>/></title>
<html:base/>
</head>
<body bgcolor=<SPAN class=java-quote>"white"</SPAN>>
<html:form action=<SPAN class=java-quote>"/LoginAction"</SPAN> focus=<SPAN class=java-quote>"name"</SPAN>>
名稱:<html:text property=<SPAN class=java-quote>"name"</SPAN> size=<SPAN class=java-quote>"20"</SPAN>/><br>
密碼:<html:password property=<SPAN class=java-quote>"password"</SPAN> size=<SPAN class=java-quote>"20"</SPAN>/><br>
<html:submit/> <html:reset/>
</html:form>
</body>
</html:html></PRE></DIV></DIV><BR> 這個頁面執行之後,有關於Action的部份將會自動被加上模組前綴,執行結果如下,請注意紅字標示部份:
<DIV class=code
style="BORDER-TOP-STYLE: solid; BORDER-RIGHT-STYLE: solid; BORDER-LEFT-STYLE: solid; BORDER-BOTTOM-STYLE: solid">
<DIV class=codeContent><PRE><html lang=<SPAN class=java-quote>"zh"</SPAN>>
<head>
<title>哈囉!Struts!</title>
<base href=<SPAN class=java-quote>"http:<SPAN class=java-comment>//localhost:8080/HelloStruts/login/login.jsp"</SPAN>></SPAN>
</head>
<body bgcolor=<SPAN class=java-quote>"white"</SPAN>>
<form name=<SPAN class=java-quote>"userForm"</SPAN> method=<SPAN class=java-quote>"post"</SPAN> action=<SPAN class=java-quote>"/HelloStruts/login/LoginAction.<SPAN class=java-keyword>do</SPAN>"</SPAN>>
名稱:<input type=<SPAN class=java-quote>"text"</SPAN> name=<SPAN class=java-quote>"name"</SPAN> size=<SPAN class=java-quote>"20"</SPAN> value=<SPAN class=java-quote>""><br>
密碼:<input type="</SPAN>password<SPAN class=java-quote>" name="</SPAN>password<SPAN class=java-quote>" size="</SPAN>20<SPAN class=java-quote>" value="</SPAN><SPAN class=java-quote>"><br>
<input type="</SPAN>submit<SPAN class=java-quote>" value="</SPAN>Submit<SPAN class=java-quote>"> <input type="</SPAN>reset<SPAN class=java-quote>" value="</SPAN>Reset<SPAN class=java-quote>">
</form>
<script type="</SPAN>text/javascript<SPAN class=java-quote>" language="</SPAN>JavaScript<SPAN class=java-quote>">
<!--
<SPAN class=java-keyword>var</SPAN> focusControl = document.forms["</SPAN>userForm<SPAN class=java-quote>"].elements["</SPAN>name<SPAN class=java-quote>"];
<SPAN class=java-keyword>if</SPAN> (focusControl.type != "</SPAN>hidden") {
focusControl.focus();
}
<SPAN class=java-comment>// --></SPAN>
</script>
</body>
</html></PRE></DIV></DIV><BR> 我們的greeting.jsp位於/login目錄下:
<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>greeting.jsp</B></DIV>
<DIV class=codeContent><PRE><%@ taglib uri=<SPAN class=java-quote>"/tags/struts-bean"</SPAN> prefix=<SPAN class=java-quote>"bean"</SPAN> %>
<%@ taglib uri=<SPAN class=java-quote>"/tags/struts-html"</SPAN> prefix=<SPAN class=java-quote>"html"</SPAN> %>
<%@ taglib uri=<SPAN class=java-quote>"/tags/struts-logic"</SPAN> prefix=<SPAN class=java-quote>"logic"</SPAN> %>
<%@page contentType=<SPAN class=java-quote>"text/html; charset=Big5"</SPAN>%>
<html:html locale=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>>
<head>
<title><bean:message key=<SPAN class=java-quote>"welcome.title"</SPAN>/></title>
<html:base/>
</head>
<body bgcolor=<SPAN class=java-quote>"white"</SPAN>>
<logic:present scope=<SPAN class=java-quote>"request"</SPAN> name=<SPAN class=java-quote>"valid_user"</SPAN>>
<bean:write name=<SPAN class=java-quote>"valid_user"</SPAN> property=<SPAN class=java-quote>"name"</SPAN>/>您好,這是您的神秘禮物!
</logic:present>
<logic:notPresent scope=<SPAN class=java-quote>"request"</SPAN> name=<SPAN class=java-quote>"valid_user"</SPAN>>
<html:link forward=<SPAN class=java-quote>"welcome"</SPAN>>請先登入</html:link>
</logic:notPresent>
</body>
</html:html></PRE></DIV></DIV><BR> 到這邊關於模組化程式的struts-config-xxx.xml設定、路徑與目錄設定就說明完畢了,剩下的是ActionForm與Action類別的撰寫,這沒什麼,為了讓您能順利完成範例並進行測試,我們列出如下而不加以說明了:
<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 + -