由于最近做的一個OA項目要用到多模塊,所以特意找了資料來參考,不過很多講的不是很詳細,為了讓像我一樣菜的初學者能明白這個多模塊配置,于是寫了本文.
Struts的官方網站上其實有示例,只要下載1.2.6以上的開發包.
其實1.1也支持多配置文件,不過實現困難,我也沒有試.
注意:我這里用的是Struts 1.2.
下面開始吧:
先說明一下,工程名為Module,分三個子模塊,分別是module1,module2,module3,為了簡單起見每一個模塊下面只放了一個含有鏈接到其它模塊的JSP文件(index.jsp),.目錄結構如下:
以下是各個文件的代碼:
Module\WebRoot\index.jsp:
<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested"%>
<logic:redirect forward="welcome"/>
沒什么內容,就是直接重定向到welcome,在這里Struts會查找WebRoot\WEB-INF\struts-config.xml下的<global-forwards>元素里,看看是否有<forward name="welcome" path="/Welcome.do" />這樣名字的轉向.
Module/WebRoot/WEB-INF/struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards>
?<forward name="welcome" path="/Welcome.do" />
</global-forwards>
<action-mappings>
?<action path="/Welcome" forward="/Welcome.jsp" />
?<action path="/toModule" type="org.apache.struts.actions.SwitchAction" />
</action-mappings>
<message-resources parameter="com.utopian.struts.ApplicationResources" />
</struts-config>
因為有<forward name="welcome" path="/Welcome.do" />,所以它就會轉向到/Welcome.do,然后Struts又會去查找到Web.xml里關于.do的處理:
Module\WebRoot\WEB-INF\web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
?<display-name>Module Test</display-name>
?<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/struts-config.xml</param-value>
??</init-param>
??<!-- module configurations -->
??<init-param>
???<param-name>config/module1</param-name>
???<param-value>/WEB-INF/module1/struts-config.xml</param-value>
??</init-param>
??<init-param>
???<param-name>config/module2</param-name>
???<param-value>/WEB-INF/module2/struts-config.xml</param-value>
??</init-param>
??<init-param>
???<param-name>config/module3</param-name>
???<param-value>/WEB-INF/module3/struts-config.xml</param-value>
??</init-param>
??<init-param>
???<param-name>debug</param-name>
???<param-value>3</param-value>
??</init-param>
??<init-param>
???<param-name>detail</param-name>
???<param-value>3</param-value>
??</init-param>
??<load-on-startup>0</load-on-startup>
?</servlet>
?<servlet-mapping>
??<servlet-name>action</servlet-name>
??<url-pattern>*.do</url-pattern>
?</servlet-mapping>
?<!-- The Usual Welcome File List -->
?<welcome-file-list>
??<welcome-file>index.jsp</welcome-file>
?</welcome-file-list>
</web-app>
注意這里,?<servlet-name>action</servlet-name>?<url-pattern>*.do</url-pattern>,根據這個,Struts會找到action.然后去struts-config.xml查找所匹配的值,因為有<action path="/Welcome" forward="/Welcome.jsp" />,/Weclcome.do就會直接轉到/Welcome.jsp.
Module\WebRoot\Welcome.jsp:
這個文件就是一些轉向:
?<ul>
????? <li><html:link module="/module1" action="/module1">這是第一個子模塊module1的鏈接</html:link></li>
????? <li><html:link module="/module2" action="/module2">這是第二個子模塊module2的鏈接</html:link></li>
????? <li><html:link module="/module3" action="/module3">這是第三個子模塊module3的鏈接</html:link></li>
? </ul>
在Struts 1.2里,如果在link標簽里用了module屬性,就表示是模塊間轉發,所以就會先到web.xml去里查找關于模塊的定義,比如說這個???<param-name>config/module1</param-name><param-value>/WEB-INF/module1/struts-config.xml</param-value>,config是表示默認的模塊,而config/則表示子模塊,這個文件夾實際上不存在.接來的情況類似前面說的查找轉向的過程.
還有什么不清楚的地方,請聯系我,我會盡我所能幫助你!
下面有完整的項目下載:
Struts 1.2多模塊開發簡單示例