Struts2设置加载非默认路径的struts.xml文件的方法

Struts2设置加载非默认路径的struts.xml文件的方法
2011年06月09日 星期四 上午 11:01
今天找了一下struts2设置加载非默认路径的struts.xml文件的方法
但很多都是一笔代过,
后来自己弄清楚了,在这里和初学的同学一起学习一下,
还请老鸟们多指教

首先我们要明白struts2加载配置文件都是从它自己的jar包
和\WEB-INF\classes两个默认的位置加载的,
如果你想改变可以在web项目的web.xml配置过虑器的时候指定
如(配置过虑器的部分代码):
XML code

 
      

<filter>

<filter-name>struts2</filter-name>

 

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

<init-param>

<param-name>config</param-name>

<param-value>struts-default.xml,struts-plugin.xml,../STRUTS-XML/struts.xml</param-value>

</init-param>

</filter>

 

我把struts.xml放在 WEB-INF\STRUTS-XML\struts.xml  window2003下试过成功,要注意的问题:1.我用".."后退退到WEB-INF目录(在很多资料中就没有说明这一点,所以有些人想放到WEB-INF目录都出错)  为不了解其它的操作系统,如果你要把struts.xml放在这些位置时要考虑这一点(兼容性)2.要注意的是,如果你设置了<param-name>config</param-name>参数,  那么struts-default.xml等原来struts2默认加载的文件你也要指定,否则它好像不会自动加载,会出错

也可以加载到struts.xml中(<include file="struts-default.xml"></include>)

(另外 struts-default.xml,struts-plugin.xml 是不用特殊指定的 struts.xml放到根目录下就可以 在struts.xml 里include 其他的struts.xml 而struts.xml 主要是进行一些配置的(比如字符类型等))3.而且使用<include file="../STRUTS-XML/user.xml"/>标签时也要系一个都要指定相对的路径和struts.xml指定一样还有一种不用这样配置的方法就是使用<include file="/STRUTS-XML/user.xml"/>也就是struts.xml放在默认的位置,只是其它的配置放到其它的地方如:

XML code
        
        

<struts>

<constantname="struts.enable.DynamicMethodInvocation"value="false"/>

<constantname="struts.devMode"value="false"/>

<includefile="STRUTS-XML/user.xml"/>

</struts>


我就是把user.xml文件放到了\WEB-INF\classes\STRUTS-XML目录下

这样也可以达到统一管理配置文件,也不用配置<param-name>config</param-name>参数

 

或者

 

在struts2的配置路径中默认是在/WEB-INF/classes下 这样可能有一些不方便,比如我们的所有配置文件在/WEB-INF/conf下面,其实配置比较简单就是如下:

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  <init-param>   
   <param-name>config</param-name>   
   <param-value>../conf/struts.xml</param-value>   
  </init-param> 
 </filter>
 <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
 </filter-mapping>

本来认为成功,结果启动不起来报错,原因是因为更改了config后会把默认的和现有的不能存在继承关系只好包含进来就可以修改struts.xml文件

<struts>

<include file="struts-default.xml" />

.....

</struts>

填上这个就可以了一切OK。

 

附加资料

使用<include>标签重用配置文件
在Struts2中提供了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用<include>标签引用这些配置文件。这样做的优点如下:

结构更清晰,更容易维护配置信息。
配置文件可以复用。如果在多个Web程序中都使用类似或相同的配置文件,那么可以使用<include>标签来引用这些配置文件,这样可以减少工作量。
假设有一个配置文件,文件名为newstruts.xml,代码如下:

<?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>
<package name="demo" extends="struts-default"
>
<action name="submit"  class="action.MoreSubmitAction">
<result name="save"
>
                /result.jsp
            </result>
<result name="print">
                /result.jsp
            </result>
</action>
</package>
</struts>

 

则struts.xml引用newstruts.xml文件的代码如下:
<?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="newstruts.xml"/>
<package name="test" extends="struts-default">
    
    </package>
</struts>


大家要注意一下,用<include>引用的xml文件也必须是完成的struts2的配置。实际上<include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值