论坛首页 Java版 Struts

Struts2的FilterDispatcher的工作原理

浏览 2932 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-08-21
我首先来说一下我的理解,大家看其中是否有理解不对的地方:大多数MVC框架struts1也好,spring MVC也好都是使用一个servlet作为前端的分发器,而struts2则使用一个过滤器来做为前端处分发器,以下是该过滤器的doFilter一段代码


public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {


        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        ServletContext servletContext = getServletContext();

        String timerKey = "FilterDispatcher_doFilter: ";
        try {           
            UtilTimerStack.push(timerKey);
            request = prepareDispatcherAndWrapRequest(request, response);
            ActionMapping mapping;
            try {
                mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());
            } catch (Exception ex) {
                LOG.error("error getting ActionMapping", ex);
                dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
                return;
            }

            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 (serveStatic && resourcePath.startsWith("/struts")) {
                	
                    String name = resourcePath.substring("/struts".length());
                    findStaticResource(name, 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);

        } finally {
            try {
                ActionContextCleanUp.cleanUp(req);
            } finally {
                UtilTimerStack.pop(timerKey);
            }
        }
    }


理解一:既然只配置过滤器而没有配置servlet,如果web.xml中配置拦截路径为*.do的话,那么调用chain.doFilter(request, response);仍然会进入该过滤器,直到找到action的mapping,也就是if(mapping==null)不成立时。
理解二:大家看整个段落的try语句,就是没有catch的那段,如果说异常可以抛出由doFilter方法抛出到调用方,finally执行回收动作的话,为什么finally中的回收代码不放置到过滤器的destroy()中呢!

PS:struts2过滤器拦截的配置是不是有bug?!我修改成*.do的话就会报错!大家有遇到过吗?
<filter>
        <filter-name>struts2</filter-name>   
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
   
最后更新时间:2007-08-21
为了革命事业,我还是冒着你的帖子被删除时,我的帖子也被删除扣分的风险回复一下。

1. strut2已经把servlet的逻辑移到了Filter中,并且这种方式更加容易支持Restful,还可以支持让你自定义ActionMapper的分解。

2. 换成*.do,我的理解是有很多struts2的资源文件你就不能用了,这些文件在jar包中,不是依.do结尾的。不过*.do应该不会造成非常大的影响,如果你不用struts2的附加功能,好比taglib之类的东西。如果有错误,请贴出错误堆栈。

3. finally 不是为了拦截异常的。ActionContextCleanUp.cleanUp(req); 是每个请求处理完以后都要调用的。ww上有解释。
   
0 请登录后投票
最后更新时间:2007-08-24
如果想用.do的话应该是在filter的init-param中增加

<init-param>
<param-name>struts.action.extension</param-name>
<param-value>do</param-value>
</init-param>

filter-mapping中仍然用

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

或者用这时候用*.do也可以,否则估计是不会认的
   
0 请登录后投票
最后更新时间:2007-09-26
也可以在struts.xml中配置
<constant name="struts.action.extension" value="do" />
   
0 请登录后投票
最后更新时间:2007-09-27
magice 写道

PS:struts2过滤器拦截的配置是不是有bug?!我修改成*.do的话就会报错!大家有遇到过吗?
<filter>
        <filter-name>struts2</filter-name>   
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>



不知道LZ测试没有,如果这里改成*.do 那么在你相应的Struts.properties文件和Struts.xml文件的Extention属性也要有do这个后缀。

而且,这个时候,你所指的报错应该是标签错误,所有的JSP页面通过Struts2.0 的 Action转发来访问就没有问题了。
   
0 请登录后投票
论坛首页 Java版 Struts

跳转论坛:
JavaEye推荐