默认情况下,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. unix网络编程-套接字编程 读书笔记

    1. 学习总结(目前只看了前6章):http://note.youdao.com/noteshare?id=2a0c29f5feeddd8f6f390427f0d67114 2. 课后习题 第一章 h ...

  2. bzoj 4206 最大团 几何+lis

    最大团 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 142  Solved: 65[Submit][Status][Discuss] Descrip ...

  3. HDU1384 差分约束

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. hbase的Region分裂代码分析

    region分裂有2种触发情景:1是用户手动触发(参见HRegionServer的splitRegion方法),2是后台flush线程flush完一个region的memstore时,会去检查这个re ...

  5. 自定义函数,根据p个数,自适应剧中效果

    //最后投保进程line-height 自适应居中; function self_adaption(){ $('.last_place').each(function(){ var _this = $ ...

  6. nodejs+react构建仿知乎的小Demo

    一.命令行进入指定项目文件夹 二.相关命令安装环境和项目工具 npm init npm install react -- save npm install -g gulp npm install -- ...

  7. eclipse好玩的插件集(一) CKEditor插件

    啥也不说,先上效果图: 当你输入完图片的url时,你可以得到预览的图像,从而进行宽高调整! 使用方法: 在eclipse市场中搜索ckeditor 配置操作如下:   进行文件关联,这样就可以直接用c ...

  8. JavaScript实现35选7并记录历史状态

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABQCAYAAACnOs9vAAAJy0lEQVR4nO2dbWwUxxnH/2c5SElQSl ...

  9. jquery checkbox选中状态以及实现全选反选

    jquery1.6以下版本获取checkbox的选中状态: $('.ck').attr('checked'); $('.ck').attr('checked',true);//全选 $('.ck'). ...

  10. 我要AFO啦好伤感啊

    我要AFO啦 虽然一直很垃圾 但是也很开心 接下来我要去学物理啦 原因是今天早上没有吃早餐?! 就这样把~ 白白