默认情况下,Struts2的配置文件名称为struts.xml,且该文件放在src根目录下。如下图所示:

如果需要修改struts.xml的位置,例如把struts.xml放到struts2文件夹下,结构如下图所示,该怎么办呢?

Struts2在web.xml中的一般配置如下:

  1. <!-- 配置struts2过滤器:StrutsPrepareAndExecuteFilter -->
  2. <filter>
  3. <filter-name>struts2</filter-name>
  4. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>struts2</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

为了弄清Stuts2是如何加载配置文件的,先查看Struts2的StrutsPrepareAndExecuteFilter类:

  1. package org.apache.struts2.dispatcher.ng.filter;
  2. /**
  3. * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use
  4. * when you don't have another filter that needs access to action context information, such as Sitemesh.
  5. */
  6. public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
  7. protected PrepareOperations prepare;
  8. protected ExecuteOperations execute;
  9. protected List<Pattern> excludedPatterns = null;
  10. public void init(FilterConfig filterConfig) throws ServletException {
  11. InitOperations init = new InitOperations();
  12. try {
  13. FilterHostConfig config = new FilterHostConfig(filterConfig);
  14. init.initLogging(config);
  15. Dispatcher dispatcher = init.initDispatcher(config);
  16. init.initStaticContentLoader(config, dispatcher);
  17. prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
  18. execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
  19. this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
  20. postInit(dispatcher, filterConfig);
  21. } finally {
  22. init.cleanup();
  23. }
  24. }
  25. /**
  26. * Callback for post initialization
  27. */
  28. protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
  29. }
  30. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  31. HttpServletRequest request = (HttpServletRequest) req;
  32. HttpServletResponse response = (HttpServletResponse) res;
  33. try {
  34. prepare.setEncodingAndLocale(request, response);
  35. prepare.createActionContext(request, response);
  36. prepare.assignDispatcherToThread();
  37. if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
  38. chain.doFilter(request, response);
  39. } else {
  40. request = prepare.wrapRequest(request);
  41. ActionMapping mapping = prepare.findActionMapping(request, response, true);
  42. if (mapping == null) {
  43. boolean handled = execute.executeStaticResourceRequest(request, response);
  44. if (!handled) {
  45. chain.doFilter(request, response);
  46. }
  47. } else {
  48. execute.executeAction(request, response, mapping);
  49. }
  50. }
  51. } finally {
  52. prepare.cleanupRequest(request);
  53. }
  54. }
  55. public void destroy() {
  56. prepare.cleanupDispatcher();
  57. }
  58. }

可以看到,有个public void init(FilterConfig filterConfig) throws ServletException { ... } 方法,很明显,这个方法在启动时会被调用,然后加载classes目录下的struts.xml配置文件。配置文件参数名称为filterConfig。因此,我们可以指定Struts2 Filter 的参数,这和指定Servlet参数相同,配置<init-param>即可。配置如下:

  1. <filter>
  2. <filter-name>struts2</filter-name>
  3. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  4. <init-param>
  5. <param-name>filterConfig</param-name>
  6. <param-value>classpath:struts2/struts.xml</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>struts2</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>

这样配置后,就可以正常加载指定的Struts配置文件了。

修改Struts2的struts.xml配置文件位置的更多相关文章

  1. struts2中struts.xml配置文件详解【未整理】

    1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管 ...

  2. struts2中struts.xml配置文件详解

    struts.xml的常用配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...

  3. struts2:struts.xml配置文件详解

    1. 几个重要的元素 1.1 package元素 package元素用来配置包.在Struts2框架中,包是一个独立的单位,通过name属性来唯一标识包.还可以通过extends属性让一个包继承另一个 ...

  4. struts2.0 struts.xml配置文件详解

    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quo ...

  5. struts2 的struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  6. struts2.0中struts.xml配置文件详解

    先来展示一个配置文件 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration ...

  7. Struts2笔记——struts.xml配置详解

    访问HelloWorld应用的路径的设置 * 在struts1中,通过<action path=“/primer/helloWorldAction.action”>节点的path属性指定访 ...

  8. Struts2初学 Struts.xml详解二

    A.使用继承实现设置全局视图    package节点中还可以设置全局的视图,如:     <global-results>         <result name="e ...

  9. struts.xml配置文件(package,namespace,action)

    struts2.0 xml配置 struts.xml文件结构 struts.xml文件是整个Struts2框架的核心. struts.xml文件内定义了Struts2的系列Action,定义Actio ...

随机推荐

  1. 树莓派使用Samba共享文件夹

    转载自:http://raspberrypihq.com/how-to-share-a-folder-with-a-windows-computer-from-a-raspberry-pi/ Shar ...

  2. mysql的concat用法

    问题提出:mybatis的mapper文件中的模糊查询: mysql CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一,下面就将为您详细介绍mysql CONCAT( ...

  3. How to configue session timeout in Hive

    This article explains how to configure the following settings in Hive:hive.server2.session.check.int ...

  4. X210串口配置与stdio移植

    串口控制器初始化关键步骤 (1)初始化串口的Tx和Rx引脚所对应的GPIO(查原理图可知Rx和Rx分别对应GPA0_1和GPA0_0) (2)GPA0CON(0xE0200000),bit[3:0] ...

  5. redis linux下的环境搭建

    系统  CentOS7 Redis 官网下载   https://redis.io/download 1.下载解压 [root@TestServer-DFJR programs]# /usr/loca ...

  6. Enterprise Architect13 : 去掉UML图页面右侧那一道竖线

    我们在使用Enterprise Architect 中,画用例图,时序图时,页面右侧有一条竖线,见下图: 如果页面元素太多,会超出竖线的范围,显得很不协调. 如果像去掉竖线,只需选择主菜单的Layou ...

  7. 珠排序Bead Sort

    珠排序非常另类[地精也很另类],看完你就知道了,先介绍思路,再分解过程 这是它的英文论文 http://www.cs.auckland.ac.nz/~jaru003/research/publicat ...

  8. 【HDU】2222 Keywords Search

    [算法]AC自动机 [题解]本题注意题意是多少关键字能匹配而不是能匹配多少次,以及可能有重复单词. 询问时AC自动机与KMP最大的区别是因为建立了trie,所以对于目标串T与自动机串是否匹配只需要直接 ...

  9. 【洛谷 P2303】 [SDOi2012]Longge的问题 (欧拉函数)

    题目链接 题意:求\(\sum_{i=1}^{n}\gcd(i,n)\) 首先可以肯定,\(\gcd(i,n)|n\). 所以设\(t(x)\)表示\(gcd(i,n)=x\)的\(i\)的个数. 那 ...

  10. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...