修改Struts2的struts.xml配置文件位置
默认情况下,Struts2的配置文件名称为struts.xml,且该文件放在src根目录下。如下图所示:
如果需要修改struts.xml的位置,例如把struts.xml放到struts2文件夹下,结构如下图所示,该怎么办呢?
Struts2在web.xml中的一般配置如下:
- <!-- 配置struts2过滤器:StrutsPrepareAndExecuteFilter -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
为了弄清Stuts2是如何加载配置文件的,先查看Struts2的StrutsPrepareAndExecuteFilter类:
- package org.apache.struts2.dispatcher.ng.filter;
- /**
- * Handles both the preparation and execution phases of the Struts dispatching process. This filter is better to use
- * when you don't have another filter that needs access to action context information, such as Sitemesh.
- */
- public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
- protected PrepareOperations prepare;
- protected ExecuteOperations execute;
- protected List<Pattern> excludedPatterns = null;
- public void init(FilterConfig filterConfig) throws ServletException {
- InitOperations init = new InitOperations();
- try {
- FilterHostConfig config = new FilterHostConfig(filterConfig);
- init.initLogging(config);
- Dispatcher dispatcher = init.initDispatcher(config);
- init.initStaticContentLoader(config, dispatcher);
- prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
- execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
- this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
- postInit(dispatcher, filterConfig);
- } finally {
- init.cleanup();
- }
- }
- /**
- * Callback for post initialization
- */
- protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
- }
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) res;
- try {
- prepare.setEncodingAndLocale(request, response);
- prepare.createActionContext(request, response);
- prepare.assignDispatcherToThread();
- if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
- chain.doFilter(request, response);
- } else {
- request = prepare.wrapRequest(request);
- ActionMapping mapping = prepare.findActionMapping(request, response, true);
- if (mapping == null) {
- boolean handled = execute.executeStaticResourceRequest(request, response);
- if (!handled) {
- chain.doFilter(request, response);
- }
- } else {
- execute.executeAction(request, response, mapping);
- }
- }
- } finally {
- prepare.cleanupRequest(request);
- }
- }
- public void destroy() {
- prepare.cleanupDispatcher();
- }
- }
可以看到,有个public void init(FilterConfig filterConfig) throws ServletException { ... } 方法,很明显,这个方法在启动时会被调用,然后加载classes目录下的struts.xml配置文件。配置文件参数名称为filterConfig。因此,我们可以指定Struts2 Filter 的参数,这和指定Servlet参数相同,配置<init-param>即可。配置如下:
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- <init-param>
- <param-name>filterConfig</param-name>
- <param-value>classpath:struts2/struts.xml</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
这样配置后,就可以正常加载指定的Struts配置文件了。
修改Struts2的struts.xml配置文件位置的更多相关文章
- struts2中struts.xml配置文件详解【未整理】
1. 深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1. 包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管 ...
- struts2中struts.xml配置文件详解
struts.xml的常用配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...
- struts2:struts.xml配置文件详解
1. 几个重要的元素 1.1 package元素 package元素用来配置包.在Struts2框架中,包是一个独立的单位,通过name属性来唯一标识包.还可以通过extends属性让一个包继承另一个 ...
- struts2.0 struts.xml配置文件详解
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quo ...
- struts2 的struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- struts2.0中struts.xml配置文件详解
先来展示一个配置文件 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration ...
- Struts2笔记——struts.xml配置详解
访问HelloWorld应用的路径的设置 * 在struts1中,通过<action path=“/primer/helloWorldAction.action”>节点的path属性指定访 ...
- Struts2初学 Struts.xml详解二
A.使用继承实现设置全局视图 package节点中还可以设置全局的视图,如: <global-results> <result name="e ...
- struts.xml配置文件(package,namespace,action)
struts2.0 xml配置 struts.xml文件结构 struts.xml文件是整个Struts2框架的核心. struts.xml文件内定义了Struts2的系列Action,定义Actio ...
随机推荐
- PostgreSQL主键索引膨胀的重建方法
普通的索引膨胀处理比较简单,主键的索引膨胀也不复杂,只是在新旧索引交替时有一些小处理.本试验在primary key上通过CONCURRENTLY建立第二索引来解决索引膨胀问题,适用9.3.9.4,其 ...
- Codeforces Round #392 (Div. 2) A B C 水 模拟 暴力
A. Holiday Of Equality time limit per test 1 second memory limit per test 256 megabytes input standa ...
- HDU4738:Caocao's Bridges(求桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- windows下安装git
1.从Git官网下载windows版本的git:http://git-scm.com/downloads 2.一般使用默认设置即可:一路next,git安装完毕! 3.但是如果这时你打开windows ...
- [ethernet]ubuntu更换网卡驱动
问题: 网络不能ping通,dmesg显示很多 [::00.0: eth0: link up [::00.0: eth0: link up [::00.0: eth0: link up [::00.0 ...
- Ubuntu14.04-Python2.7-Virtualenv-Django1.9-MySQL完整环境配置
一.安装Ubuntu14.04LTS 1.下载了ubuntu14.04后用ultraISO写到硬盘镜像(U盘) 开机启动项改成U盘在前,安装. 清空分区,重新分配. /最少10G,我放了100G. 物 ...
- 【设计模式】 模式PK:装饰模式VS适配器模式
1.概述 装饰模式和适配器模式在通用类图上没有太多的相似点,差别比较大,但是它们的功能有相似的地方:都是包装作用,都是通过委托方式实现其功能.不同点是:装饰模式包装的是自己的兄弟类,隶属于同一个家族( ...
- 【uva11987】带删除的并查集
题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...
- 【BZOJ3875】【AHOI2014】骑士游戏 [Spfa][DP]
骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 在这个游戏中,JYY一共有两种攻击 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...