struts配置文件l中使用include 标签 引入另外一个在jar中的struts文件

来源:互联网 发布:c语言中char 编辑:程序博客网 时间:2024/05/22 15:45

最近遇到了一个需求:有一个master struts.xml, 该文件中需要引入若该子模块中struts.xml.

但是这些子模块都以jar包的方式放入主模块的classpath中,当然它们的struts.xml也在jar中, 这种情形主模块的struts还能引入吗?

首先问问google,结果是可行, 如下来自struts官方网站:

[html] view plaincopyprint?
  1. <!DOCTYPE struts PUBLIC  
  2.   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.   "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.     <include file="Home.xml"/>  
  6.     <include file="Hello.xml"/>  
  7.     <include file="Simple.xml"/>  
  8.     <include file="/util/POJO.xml"/>  
  9.     <include file="/com/initech/admin/admin-struts.xml"/>  
  10. </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 theDOCTYPE.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标签,并填入全路径, 如下。

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <include file="struts-default.xml" />  
  7.     <include file="com/strutsTest/myTest/test-struts.xml" />  
  8. </struts>  
<?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标签,只填入文件名, 如下。

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <include file="struts-default.xml" />  
  7.     <include file="test-struts.xml" />  
  8. </struts>  
<?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的顶级路径中, 即:

[html] view plaincopyprint?
  1. childModule.jar  
  2. -com  
  3.         --strutsTest  
  4.               --.....  
  5. test-struts.xml  
childModule.jar-com        --strutsTest              --.....test-struts.xml

主struts配置文件与2相同

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <include file="struts-default.xml" />  
  7.     <include file="test-struts.xml" />  
  8. </struts>  
<?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>
结果: 成功!
注意如果要加载本地struts的xml必须使用全包名字。
原创粉丝点击