我们在开发web项目的时候,一般会用到很多的配置文件,特别是在使用struts2,spring等框架时。为了方便的管理所有的配置文件,我们可以把所有的配置文件放在一起。在使用中Struts框架的时候可以在创建工程的时候除了src目录外,直接添加一个config的目录,专门用来存放我们的配置文件。注意这个config文件夹必须是一个source folder,如果在创建工程的时候未创建config目录可以在后期添加,但是必须保证其为source folder,否则加载不到里面的配置文件。
在config目录下创建struts.xml,作为struts总的配置文件,其余实现具体功能的配置文件可以通过include的方式引入到struts.xml。工程的目录情况可参考如下:
如何编写web.xml?按照一般的方式编写该文件,涉及到struts'的部分,需要移除掉关于struts配置部分的配置代码。因为struts.xml是在一个source folder下,会被默认加载,不需要我们手动指定。
web.xml中关于struts2的配置,可参考如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssmstudy</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 不需要指定struts2配置文件的位置 -->
<!-- <init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>