(一)默认情况下,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>
本文详细介绍了Struts2框架中配置文件struts.xml的默认位置及其如何通过web.xml进行自定义配置的方法。包括如何放置配置文件于不同目录,并确保Struts2框架能够正确加载。

468

被折叠的 条评论
为什么被折叠?



