`
ssydxa219
  • 浏览: 627747 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

struts2 handle static resource

 
阅读更多

在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 resource

    综上所述,“struts2 resource”涉及的是Struts2框架的核心概念和特性,理解和掌握这些知识点对于开发基于Struts2的Java web应用至关重要。通过熟练运用这些技术,开发者可以构建出高效、稳定且易于维护的Web应用...

    Struts2漏洞检查工具Struts2.2019.V2.3

    Struts2是一款非常流行的Java Web框架,用于构建企业级应用。然而,随着时间的推移,Struts2在安全方面暴露出了一些重要的漏洞,这给使用该框架的系统带来了潜在的安全风险。"Struts2漏洞检查工具Struts2.2019.V2.3...

    struts2jar包

    Struts2是一个强大的Java EE应用程序框架,主要用于构建企业级的Web应用。它的核心是MVC(Model-View-Controller)设计模式,可以帮助开发者组织代码,提高开发效率,并且提供了丰富的特性来支持表单验证、国际化、...

    struts2-core.jar

    struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...

    Struts2漏洞测试

    Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试...

    struts2 API帮助文档

    - Struts2提供对多语言环境的支持,通过资源包(Resource Bundles)管理不同语言的文本和消息。 9. **异常处理(Exception Handling)** - 框架提供了一套异常处理机制,允许自定义错误页面和处理策略。 10. **测试...

    struts2 总结工程大全

    struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全...

    struts2 最新漏洞 S2-016、S2-017修补方案 .docx

    Struts2 最新漏洞 S2-016、S2-017 修补方案 Struts2 是一个基于 Java 的 Web 应用程序框架,由 Apache 软件基金会维护。最近,Struts2 发生了两个严重的漏洞,分别是 S2-016 和 S2-017,这两个漏洞可能会导致攻击者...

    Struts2接口文档

    Struts2是一个强大的Java web应用程序开发框架,它基于Model-View-Controller(MVC)设计模式,旨在简化创建用户交互式、数据驱动的web应用的过程。这个“Struts2接口文档”是开发者的重要参考资料,提供了关于...

    struts2项目开发

    Struts2 项目开发 Struts2 是一个基于 Java Web 的框架,广泛应用于 Web 应用程序的开发。下面将从 Struts2 项目开发的角度,详细介绍 Struts2 框架的应用、开发流程、技术架构、实践经验等方面的知识点。 项目...

    Struts2视频教程

    ### Struts2核心知识点解析 #### 一、Struts2框架概述 - **定义与特点**:Struts2是一款基于MVC(Model-View-Controller)设计模式的Java Web应用程序框架,它继承了Struts1的优点,同时在设计上更加灵活、易用,...

    Struts2的常量详解

    Struts2 的常量详解 Struts2 框架中有许多重要的常量,它们负责控制着 Struts2 的行为和配置。下面我们将对 Struts2 中的一些常量进行详细的解释和分析。 1. struts.configuration struts.configuration 属性指定...

    struts2-showcase.rar

    Struts2-showcase是一个用于演示和学习Apache Struts2框架功能的开源项目。这个压缩包“struts2-showcase.rar”包含了完整的源代码,旨在帮助开发者深入理解Struts2框架的工作原理及其各种特性。以下是对Struts2和...

    Struts2教学视频

    Struts2是一个强大的Java web开发框架,用于构建可维护、可扩展且结构良好的应用程序。它在MVC(Model-View-Controller)设计模式的基础上提供了一种实现方式,使得开发者能够更方便地处理用户请求,控制业务逻辑,...

    Struts2VulsTools-Struts2系列漏洞检查工具

    该工具的打开路径为:\Struts2VulsTools-2.3.20190927\Test\bin\Release\Text.exe 2019-09-25: 优化部分EXP在部分情况下被WAF拦截的问题,提高检测成功率,优化自定义上传路径exp,文件所在目录不存在时自动创建...

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板 struts2环境搭建:基础框架搭建,简单易上手,适合新手,比你看书强多了,实践源于理论而高于理论,贵在实践 朋友。 struts2 新闻发布系统:struts2+jsp 功能不...

    Struts2_s2-016&017&ognl2.6.11_patch漏洞补丁

    -- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper --&gt; &lt;bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class=...

    struts2项目部署

    Struts2是一个流行的Java web框架,它为开发者提供了一种模型-视图-控制器(MVC)架构,便于创建结构清晰、可维护性高的企业级应用程序。本教程将详细介绍如何部署一个基于Struts2的项目,特别适合初学者。由于实际...

    struts2技术内幕+struts2权威指南

    《Struts2技术内幕:深入解析Struts2架构设计与实现原理》以Struts2的源代码为依托,通过对Struts2的源代码的全面剖析深入探讨了Struts2的架构设计、实现原理、设计理念与设计哲学,对从宏观上和微观上去了解Struts2...

    struts2帮助文档

    struts2的概述struts2下的HelloWord struts2基础 struts2做好准备 struts2的标志 struts2的Action讲解 struts2中的国际化 struts2转化器 struts2实现表单数据校验 struts2的基石-拦截器 struts2中实现IOC struts2中...

Global site tag (gtag.js) - Google Analytics