struts2配置文件struts.xml的存放位置

(一)默认情况下,Struts2的配置文件名称为struts.xml,且该文件放在src根目录下。

(二)如果需要修改struts.xml的位置,可以在web.xml中配置,使用<init-param/> 标签来配置,使用相对路径配置

例如:将struts.xml文件放在src-struts文件下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


<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/struts.xml</param-value>
</init-param>

</filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

1、Struts2默认加载文件为struts.xml,struts-default.xml,struts-plugin.xml
2、init-param的param-name必须叫config,不能叫filterConfig等其他名字,否则会报错
3、
init-param的param-value必须有struts-defalut.xml,如果与Spring集成使用到了包struts2-spring-plugin还必须有struts-plugin.xml
4、此处文件路径相对于
/WEB-INF/classes/若为下级子目录,也许加子目录名,如/WEB-INF/classes/config,则param-value需要写作classpath:config/struts.xml

 

(三)如果将struts.xml放到WEB-INF下,配置如下:因为默认是在WEB-INF/classes/文件夹下  ,因此要使用../上级目录才可以

 

<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.xml</param-value>
</init-param>

</filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一位远方的诗人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值