在web.xml中作如下配置:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
         <param-name>packages</param-name>
         <param-value>net.zdsoft.eis.template</param-value>
        </init-param>
    </filter>

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping> <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/static/*</url-pattern>
 </filter-mapping>

用于处理静态资源(css、js、图片之类),启动后正常。但当修改了struts配置文件,并且struts.configuration.xml.reload=true时,再次加载静态资源时会出错如下错误:

java.lang.NullPointerException at org.apache.struts2.dispatcher.DefaultStaticContentLoader.findStaticResource(DefaultStaticContentLoader.java:164)
 at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeStaticResourceRequest(ExecuteOperations.java:62)
 at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:86)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 
原因:重新加载配置文件时,静态资源的处理类DefaultStaticContentLoader的setHostConfig(HostConfig filterConfig)方法没调用,导致pathPrefixes为空。

解决方案:

1、继承StrutsPrepareOperations类,构造方法中传入参数FilterConfig。createActionContext方法中如果oldContext为空,则重新初始化静态资源配置。完整代码如下:

public class StrutsPrepareOperations extends PrepareOperations {
    private ServletContext servletContext;
    private Dispatcher dispatcher;
    private FilterHostConfig config;

public StrutsPrepareOperations(ServletContext servletContext, Dispatcher dispatcher,
            FilterConfig filterConfig) {
        super(servletContext, dispatcher);
        this.dispatcher = dispatcher;
        this.servletContext = servletContext;
        this.config = new FilterHostConfig(filterConfig);
    }

// 重写doFilter,由于在struts配置文件重新加载后,静态资源有问题
    /**
     * Creates the action context and initializes the thread local
     */
    public ActionContext createActionContext(HttpServletRequest request,
            HttpServletResponse response) {
        ActionContext ctx;
        Integer counter = 1;
        Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
        if (oldCounter != null) {
            counter = oldCounter + 1;
        }

ActionContext oldContext = ActionContext.getContext();
        if (oldContext != null) {
            // detected existing context, so we are probably in a forward
            ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
        } else {
            ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class)
                    .createValueStack();
            stack.getContext().putAll(
                    dispatcher.createContextMap(request, response, null, servletContext));
            ctx = new ActionContext(stack.getContext());

// add by zhaosf
            StaticContentLoader staticResourceLoader = dispatcher.getContainer().getInstance(
                    StaticContentLoader.class);
            staticResourceLoader.setHostConfig(config);
        }
        request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
        ActionContext.setContext(ctx);
        return ctx;
    }

2、继承org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter时,重写init方法,如下:

public class StrutsFilter extends StrutsPrepareAndExecuteFilter {

@Override
    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 StrutsPrepareOperations(filterConfig.getServletContext(), dispatcher,
                    filterConfig);
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
            this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);

postInit(dispatcher, filterConfig);
        } finally {
            init.cleanup();
        }
    }

struts2.1.8 StrutsPrepareAndExecuteFilter 关于静态资源处理问题。访问classPath下静态资源相关推荐

  1. 访问WEB-INF下的资源

    WEB-INF下的资源是受保护的,我们无法通过http://localhost:8080/xmm/WEB-INF/index.jsp的方式直接访问WEB-INF下的资源,但是可以通过controlle ...

  2. 访问不到webapps的html,如何访问tomcat的webapps下的资源

    最近搭建文件服务器,发现一个问题,如果访问localhost:8080/项目名/ 下的图片是可以的,但是直接访问localhost:8080/ 下的资源会显示资源没有权限. 那么如何才能直接访问web ...

  3. Spring-Boot:写出来的网站访问不到静态资源?怎样通过url访问SpringBoot项目中的静态资源?localhost:8989/favicon.ico访问不了工程中的图标资源?

    Spring-Boot:Spring-Boot写出来的网站访问不到静态资源?怎样通过url访问SpringBoot项目中的静态资源?localhost:8989/favicon.ico访问不了工程中的 ...

  4. Spring Boot——自定义Web配置类后无法访问/static文件夹下静态资源

    问题描述 自定义Web配置类后无法访问 /static文件夹下静态资源. 已加相关依赖包. 官方文档 Spring MVC Auto Configuration Maven <dependenc ...

  5. django 怎么加权限 静态资源目录_Django1.7如何配置静态资源访问

    Django是非常轻量级的Web框架,今天散仙来看下如何在Django中配置静态的资源访问路径,一个中等规模的网站,可能就会有很多静态的资源需要访问,无论是html,txt,还是压缩包,有时候访问这些 ...

  6. Springboot配置通过URL访问图片(静态资源)

    一.确保为web项目,创建WebConfig 实现WebMvcConfigurer (推荐)或 继承WebMvcConfigurationSupport 说明: SpringBoot2.0 配置Web ...

  7. Spring MVC访问不到静态资源

    运行Spring MVC项目,发现.css,js等文件加载不了,一般是spring MVC的拦截匹配导致的. 例如我在web.xml里这样配置: <servlet><servlet- ...

  8. linux apache 跨域,解决nginx/apache静态资源跨域访问问题详解

    1. apache静态资源跨域访问 找到apache配置文件httpd.conf 找到这行 #LoadModule headers_module modules/mod_headers.so 把#注释 ...

  9. Springboot 页面访问不到静态资源Failed to load resource: the server responded with a status of 404 ()

    Springboot 页面访问不到静态资源 问题:在HTML文件中引入图片,但是浏览器访问不到图片. index.html: <!DOCTYPE html> <html xmlns: ...

最新文章

  1. [收集] Web服务相关的, 介绍框架(framework)类的论文
  2. Java web 环境搭建-Linux
  3. 微软、苹果把未来押注 FPGA?凭什么
  4. 神策数据荣膺 2017 企业创新典范、最佳青年榜样双殊荣
  5. Java将一段逗号分割的字符串转换成一个数组(亲测)
  6. C# 基础知识 (一).概念与思想篇
  7. input表单只允许输入大于0的整数
  8. 人才认证+奖金,智能分拣挑战赛baseline助力最后冲刺
  9. java(14) - HashMap类
  10. python类:class创建、数据方法属性及访问控制(下划线)
  11. ASP.NET数据分页技术(4)
  12. ET vs Ad hoc
  13. 米聊之死 雷军的“猪”折了腿?
  14. css绘制梯形图形,及显示矩形图片
  15. 第四章 Cesium学习入门之加载离线影像图(tif)
  16. mysql的partition_MySQL分区(Partition)
  17. 计算机老是卡顿怎么解决,电脑反应太慢怎么处理_电脑卡顿什么原因-win7之家
  18. 区块链技术:未来将颠覆的9大行业
  19. 笔记 - JavaScript - 超哥视频
  20. 讨论-职场中年人,你的危机感都来源于哪里

热门文章

  1. 单片机-定时/计数器原理功能介绍
  2. 修复分区表恢复数据图文教程
  3. C语言程序设计三大基本结构之循环结构
  4. 《AUTOSAR谱系分解(ETAS工具链)》之DCM的子模块DSL、DSD和DSP
  5. arduino控制震动传感器
  6. Millionaire.java
  7. android系统数据业务知识点总结(一)
  8. C语言实现随机抢红包功能,抢红包速度大幅提升,一个亿的机会!
  9. iwanna用哪个计算机语言,I wanna 完全新手教学 ver1.0
  10. 编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数