Struts学习

org.apache.struts2.dispatcher.FilterDispatcher的四个功能
1.org.apache.struts2.dispatcher.FilterDispatcher?
    是Struts2的主要的Filter,负责四个方面的功能:
        (1)执行Actions
        (2)清除ActionContext
        (3)维护静态内容
        (4)清除request生命周期内的XWork的interceptors(拦截者)
    另注:该过滤器应该过滤所有的请求URL。一般被设置为/*.
    具体:
        (1)执行Actions
            过滤器通过ActionMapper对象,来判断是否应该被映射到Action.如果mapper对象指示他应该被映射,过滤链将会被终止, 然后Action被调用。这一点非常重要,如果同时使用SiteMesh filter,则SiteMesh filter应该放到该过滤器前,否则 Action的输出将不会被装饰。
        (2)清除ActionContext
            过滤器为了确保内存溢出,会自动的清除ActionContext。这可能会存在一些问题,在和其它的框架集成时,例如SiteMesh。
            ActionContextCleanUp提供了怎么处理这些问题的一些信息。
        (3)维护静态内容
            过滤器也会维护在Struts2中使用的一些公共的静态的内容,例如JavaScript文件,CSS文件等。搜索/struts/*范围内的请求,然后将/struts/后面的值映射到一些struts的公共包中,也可以在你的类路径中搜索。默认情况下会去查找以下包:

StrutsPrepareAndExecuteFilter介绍

package org.apache.struts2.dispatcher.ng.filter;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.ng.PrepareOperations;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.dispatcher.mapper.ActionMapping;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* 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 {
    private PrepareOperations prepare;
    private ExecuteOperations execute; 
//初始化过滤器
    public void init(FilterConfig filterConfig) throws ServletException {
        InitOperations init = new InitOperations(); //初始化辅助对象,封装了初始化的一些操作
        try {
            FilterHostConfig config = new FilterHostConfig(filterConfig); //对filterConfig进行封装
            init.initLogging(config); //通过config,初始化内部Struts的记录


            Dispatcher dispatcher = init.initDispatcher(config); //通过config,创建并初始化dispatcher

 

            init.initStaticContentLoader(config, dispatcher); //通过config和dispatcher,初始化与过滤器相关的静态内容加载器

            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher); //通过config和dispatcher,创建request被处理前的系列操作对象
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);//通过config和dispatcher,创建处理request的系列操作对象
 
        } finally {
            init.cleanup(); //清空ActionContext
        }

    }

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

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        try {
            prepare.createActionContext(request, response); //创建ACTIONCONTEXT,并初始化Theadlocal 

            prepare.assignDispatcherToThread(); //指派dispatcher给Theadlocal
            prepare.setEncodingAndLocale(request, response); //设置request的编码和LOCAL
            request = prepare.wrapRequest(request); //封装request
            ActionMapping mapping = prepare.findActionMapping(request, response); //查找并选择创建ActionMapping
            if (mapping == null) { //如果映射不存在
                boolean handled = execute.executeStaticResourceRequest(request, response); //试图执行一个静态资源的请求
                if (!handled) {
                    chain.doFilter(request, response);
                }
            } else { //如果存在映射
                execute.executeAction(request, response, mapping); //执行action
            }
        } finally {
            prepare.cleanupRequest(request); //清除request的Threadlocal
        }
    }

    public void destroy() {
        prepare.cleanupDispatcher();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值