xzai03 2010-06-07 16:22
浏览 531
已采纳

Struts2项目中配置的自定义过滤器无效

Struts2的核心过滤器不用多说,配置如下

[code="java"]

Struts2
org.apache.struts2.dispatcher.FilterDispatcher


Struts2
/*

[/code]

此外,还配置了一个自定义的过滤器,*.html (我的Struts2请求后缀是html)

结果没有进入这个过滤器,把*.html改成/*就行了,但是这个过滤器只负责拦截html,如果写/*的话所有都会拦截,比如js, css等,这样就不符合本意了,请指点。

拦截器就不用说了,我暂时不考虑用拦截器,不想因为是Struts2就放弃了原先的过滤器。

请都先看清楚了再回答,没素质的疯子远离,谢谢

  • 写回答

3条回答 默认 最新

  • myali88 2010-06-09 17:04
    关注

    我按你的意思试了试,我的过滤器配置成这样:
    [code="xml"]

    encoding
    .action


    struts2
    /


    some
    .action

    [/code]
    其他部分我就忽略了,这种情况下,过滤器分别经过“encoding”、“struts2”,没有进入“some”。如果采用下面的配置:
    [code="xml"]

    encoding
    .action


    some
    .action


    struts2
    /

    [/code]
    则分别经过“encoding”、“some”、“struts”。问题的关键在于过滤器中的doFilter方法中是否执行了:
    [code="java"]
    chain.doFilter(request, response);
    [/code]
    即进入到下一个过滤器。而从FilterDispatcher的实现来看,它是有可能不执行这一句的:
    [code="java"]
    ...
    if (mapping == null) {
    // there is no action in this request, should we look for a static resource?
    String resourcePath = RequestUtils.getServletPath(request);

                if ("".equals(resourcePath) && null != request.getPathInfo()) {
                    resourcePath = request.getPathInfo();
                }
    
                if (staticResourceLoader.canHandle(resourcePath)) {
                    staticResourceLoader.findStaticResource(resourcePath, request, response);
                } else {
                    // this is a normal request, let it pass through
                    chain.doFilter(request, response);
                }
                // The framework did its job here
                return;
            }
    
            dispatcher.serviceAction(request, response, servletContext, mapping);
    

    ...
    [/code]
    这里可以看出是否进入下一个过滤器是有条件的。
    这就是我分析的结论,不知道是否能解答你的问题?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗