struts 2.1.6 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();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章