⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 入門 11 - 協同開發 - 模組化程式 - javaworld@tw.htm

📁 struts快速入门文章
💻 HTM
📖 第 1 页 / 共 3 页
字号:
            <DIV class=codeContent><PRE>&lt;struts-config&gt;
    &lt;global-forwards&gt;
        &lt;forward
            name=<SPAN class=java-quote>"welcome"</SPAN>
            path=<SPAN class=java-quote>"/Welcome.<SPAN class=java-keyword>do</SPAN>"</SPAN>/&gt;
    &lt;/global-forwards&gt;
                                                                                               
    &lt;global-forwards&gt;
        &lt;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>/&gt;
    &lt;/global-forwards&gt;
                                                                                               
    &lt;action-mappings&gt;
        &lt;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>/&gt;
    &lt;/action-mappings&gt;

    &lt;message-resources parameter=<SPAN class=java-quote>"resources.application"</SPAN>/&gt;
&lt;/struts-config&gt;</PRE></DIV></DIV><BR> 在&lt;global-forward&gt;的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>&lt;%@ taglib uri=<SPAN class=java-quote>"/tags/struts-bean"</SPAN> prefix=<SPAN class=java-quote>"bean"</SPAN> %&gt;
&lt;%@ taglib uri=<SPAN class=java-quote>"/tags/struts-html"</SPAN> prefix=<SPAN class=java-quote>"html"</SPAN> %&gt;
&lt;%@page contentType=<SPAN class=java-quote>"text/html; charset=Big5"</SPAN>%&gt;
&lt;html:html locale=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>&gt;
&lt;head&gt;
&lt;title&gt;&lt;bean:message key=<SPAN class=java-quote>"welcome.title"</SPAN>/&gt;&lt;/title&gt;
&lt;html:base/&gt;
&lt;/head&gt;
&lt;body bgcolor=<SPAN class=java-quote>"white"</SPAN>&gt;
                                                                                               
歡迎!&lt;html:link forward=<SPAN class=java-quote>"login"</SPAN>&gt;請先登入&lt;/html:link&gt;
                                                                                               
&lt;/body&gt;
&lt;/html:html&gt;</PRE></DIV></DIV><BR> 在&lt;html:link&gt;的設定中,將會查找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>&lt;html lang=<SPAN class=java-quote>"zh"</SPAN>&gt;
&lt;head&gt;
&lt;title&gt;哈囉!Struts!&lt;/title&gt;
&lt;base href=<SPAN class=java-quote>"http:<SPAN class=java-comment>//localhost:8080/HelloStruts/pages/Welcome.jsp"</SPAN>&gt;</SPAN>
&lt;/head&gt;
&lt;body bgcolor=<SPAN class=java-quote>"white"</SPAN>&gt;

歡迎!&lt;a href=<SPAN class=java-quote>"/HelloStruts/login/Login.<SPAN class=java-keyword>do</SPAN>"</SPAN>&gt;請先登入&lt;/a&gt;

&lt;/body&gt;
&lt;/html&gt;</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>&lt;struts-config&gt;
    &lt;form-beans&gt;
        &lt;form-bean
            name=<SPAN class=java-quote>"userForm"</SPAN>
            type=<SPAN class=java-quote>"onlyfun.caterpillar.UserForm"</SPAN>/&gt;
    &lt;/form-beans&gt;
                                                                                               
    &lt;global-forwards&gt;
        &lt;forward
            name=<SPAN class=java-quote>"login"</SPAN>
            path=<SPAN class=java-quote>"/Login.<SPAN class=java-keyword>do</SPAN>"</SPAN>/&gt;
    &lt;/global-forwards&gt;
                                                                                               
    &lt;action-mappings&gt;
        &lt;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>/&gt;
                                                                                               
        &lt;action
            path=<SPAN class=java-quote>"/LoginAction"</SPAN>
            type=<SPAN class=java-quote>"onlyfun.caterpillar.LoginAction"</SPAN>
            name=<SPAN class=java-quote>"userForm"</SPAN>&gt;
            &lt;forward
                name=<SPAN class=java-quote>"greeting"</SPAN>
                path=<SPAN class=java-quote>"/greeting.jsp"</SPAN>/&gt;
        &lt;/action&gt;
    &lt;/action-mappings&gt;
                                                                                               
    &lt;message-resources parameter=<SPAN class=java-quote>"resources.application"</SPAN>/&gt;
&lt;/struts-config&gt;</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>&lt;%@ taglib uri=<SPAN class=java-quote>"/tags/struts-bean"</SPAN> prefix=<SPAN class=java-quote>"bean"</SPAN> %&gt;
&lt;%@ taglib uri=<SPAN class=java-quote>"/tags/struts-html"</SPAN> prefix=<SPAN class=java-quote>"html"</SPAN> %&gt;
&lt;%@page contentType=<SPAN class=java-quote>"text/html; charset=Big5"</SPAN>%&gt;
&lt;html:html locale=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>&gt;
&lt;head&gt;
&lt;title&gt;&lt;bean:message key=<SPAN class=java-quote>"welcome.title"</SPAN>/&gt;&lt;/title&gt;
&lt;html:base/&gt;
&lt;/head&gt;
&lt;body bgcolor=<SPAN class=java-quote>"white"</SPAN>&gt;
                                                                                               
&lt;html:form action=<SPAN class=java-quote>"/LoginAction"</SPAN> focus=<SPAN class=java-quote>"name"</SPAN>&gt;
    名稱:&lt;html:text property=<SPAN class=java-quote>"name"</SPAN> size=<SPAN class=java-quote>"20"</SPAN>/&gt;&lt;br&gt;
    密碼:&lt;html:password property=<SPAN class=java-quote>"password"</SPAN> size=<SPAN class=java-quote>"20"</SPAN>/&gt;&lt;br&gt;
    &lt;html:submit/&gt; &lt;html:reset/&gt;
&lt;/html:form&gt;
                                                                                               
&lt;/body&gt;
&lt;/html:html&gt;</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>&lt;html lang=<SPAN class=java-quote>"zh"</SPAN>&gt;
&lt;head&gt;
&lt;title&gt;哈囉!Struts!&lt;/title&gt;
&lt;base href=<SPAN class=java-quote>"http:<SPAN class=java-comment>//localhost:8080/HelloStruts/login/login.jsp"</SPAN>&gt;</SPAN>
&lt;/head&gt;
&lt;body bgcolor=<SPAN class=java-quote>"white"</SPAN>&gt;

&lt;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>&gt;
    名稱:&lt;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>""&gt;&lt;br&gt;
    密碼:&lt;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>"&gt;&lt;br&gt;

    &lt;input type="</SPAN>submit<SPAN class=java-quote>" value="</SPAN>Submit<SPAN class=java-quote>"&gt; &lt;input type="</SPAN>reset<SPAN class=java-quote>" value="</SPAN>Reset<SPAN class=java-quote>"&gt;
&lt;/form&gt;
&lt;script type="</SPAN>text/javascript<SPAN class=java-quote>" language="</SPAN>JavaScript<SPAN class=java-quote>"&gt;
  &lt;!--
  <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>// --&gt;</SPAN>
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</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>&lt;%@ taglib uri=<SPAN class=java-quote>"/tags/struts-bean"</SPAN> prefix=<SPAN class=java-quote>"bean"</SPAN> %&gt;
&lt;%@ taglib uri=<SPAN class=java-quote>"/tags/struts-html"</SPAN> prefix=<SPAN class=java-quote>"html"</SPAN> %&gt;
&lt;%@ taglib uri=<SPAN class=java-quote>"/tags/struts-logic"</SPAN> prefix=<SPAN class=java-quote>"logic"</SPAN> %&gt;
&lt;%@page contentType=<SPAN class=java-quote>"text/html; charset=Big5"</SPAN>%&gt;
&lt;html:html locale=<SPAN class=java-quote>"<SPAN class=java-keyword>true</SPAN>"</SPAN>&gt;
&lt;head&gt;
&lt;title&gt;&lt;bean:message key=<SPAN class=java-quote>"welcome.title"</SPAN>/&gt;&lt;/title&gt;
&lt;html:base/&gt;
&lt;/head&gt;
&lt;body bgcolor=<SPAN class=java-quote>"white"</SPAN>&gt;
                                                                                               
&lt;logic:present scope=<SPAN class=java-quote>"request"</SPAN> name=<SPAN class=java-quote>"valid_user"</SPAN>&gt;
    &lt;bean:write name=<SPAN class=java-quote>"valid_user"</SPAN> property=<SPAN class=java-quote>"name"</SPAN>/&gt;您好,這是您的神秘禮物!
&lt;/logic:present&gt;
                                                                                               
&lt;logic:notPresent scope=<SPAN class=java-quote>"request"</SPAN> name=<SPAN class=java-quote>"valid_user"</SPAN>&gt;
    &lt;html:link forward=<SPAN class=java-quote>"welcome"</SPAN>&gt;請先登入&lt;/html:link&gt;
&lt;/logic:notPresent&gt;
                                                                                               
&lt;/body&gt;
&lt;/html:html&gt;</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 + -