最近遇到了一個需求:有一個master struts.xml, 該檔案中需要引入若該子模組中struts.xml.
但是這些子模組都以jar包的方式放入主模組的classpath中,當然它們的struts.xml也在jar中, 這種情形主模組的struts還能引入嗎?
首先問問google,結果是可行, 如下來自struts官方網站:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <include file="Home.xml"/> <include file="Hello.xml"/> <include file="Simple.xml"/> <include file="/util/POJO.xml"/> <include file="/com/initech/admin/admin-struts.xml"/></struts>
Each included file must be in the same format as struts.xml, including the
DOCTYPE. The include files can be placed anywhere on the classpath and should be referred to by that path by the "file" attribute.
開始嘗試:
子模組的struts設定檔 名為test-struts.xml, 路徑為com/strutsTest/myTest/
1. 在主struts設定檔中使用include標籤,並填入全路徑, 如下。
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml" /><include file="com/strutsTest/myTest/test-struts.xml" /></struts>
結果:撲街
2. 在主struts設定檔中使用include標籤,只填入檔案名稱, 如下。
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml" /><include file="test-struts.xml" /></struts>
結果:撲街
3. 在主struts設定檔中使用include標籤,只填入檔案名稱 並將子模組中的struts設定檔放入jar的頂級路徑中, 即:
childModule.jar-com --strutsTest --.....test-struts.xml
主struts設定檔與2相同
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml" /><include file="test-struts.xml" /></struts>
結果: 成功!
可是為什麼呢,暫記錄,等有時間解釋,並期待大牛的答疑。