JFinalFilter

一、入口->JFinalFilter 位於 web.xml 實現了 javax.servlet.Filter 接口java

屬性: Handler、web

            Encoding、app

             JfinalConfig、工具

            Constants、ui

             Jfinal、spa

            Logger 插件

等。日誌

方法:init() xml

    (1)繼承

        //從web.xml文件中,根據configClass查找繼承 JFinalConfig 的自定義類

        CreateJFinalConfig(filterConfig.getInitParameter("configClass"));

        - >用於初始化 JfinalConfig 失敗則拋出異常

    (2)

        //jfinalConfig在上面已經進行初始化。方法返回Boolean判斷初始化成功否

        Jfinal.init(jfinalConfig, filterConfig.getServletContext())

        ->進入該方法,能夠看到:

            <1>

            This.servletContext = servletContext;

            <2>

            This.contextPath = servletContext.getContextPath();

            <3>

             InitPathUtil();

            ->獲取項目相對路徑

            <4>

            Config.configJFinal(jfinalConfig);

            ->執行了自定義 DemoConfig 的 7 個方法

                其中

                JfinalConfig.configConstant(constants);

                InitLoggerFactory();

                ->用於初始化日誌工具

                JfinalConfig.configRoute(routes);

                JfinalConfig.configPlugin(plugins);

                StartPlugins(); // very important!!!

                ->用於啓動插件

                JfinalConfig.configInterceptor(interceptors);

                JfinalConfig.configHandler(handlers);

            ->其餘 5 個方法用於給屬性 Constants

                                                        Routes

                                                        Plugins

                                                        Interceptors

                                                        Handlers

                 賦值。

            <5>

                Constants = Config.getConstants();

                ->屬性 Constants 用於配置常量 下面的幾個方法須要用到加載的常量

            <6>

                InitActionMapping();

                ->這個方法重要的地方,在於裏面的 BuildActionMapping() 方法。

                    涉及到 Action 的映射。一者加載 Routes 兩者加載 Interceptors


            <7>

                 InitHandler();

                ->用於初始化處理器 (暫時沒有怎麼使用過,需進一步瞭解)

            <8>

                InitRender();

                ->這個方法重要的地方,在於裏面的 

                    RenderFactory.init(constants, servletContext) 方法。

                    涉及到多種視圖的初始化,例如:

                        InitFreeMarkerRender(servletContext);

                         InitVelocityRender(servletContext);

                        InitJspRender(servletContext);

                        InitFileRender(servletContext);

                    等。


            <9>

                InitOreillyCos();

                ->初始化文件上傳的相關設置

                涉及到的類有     MultipartRequest

                                          OreillyCos

                等。

            <10>

                InitI18n();

                    ->初始化國際化相關信息 

            <11>

                InitTokenManager();

                ->初始化 Token 信息,若是不設置 setTokenCache 

                    則默認使用 HttpSession 生成 token

        (3)

        Handler=jfinal.getHandler();


        (4)

        Constants=Config.getConstants();


        (5)

        Encoding=constants.getEncoding();


        (6)

        JfinalConfig.afterJFinalStart();

方法:doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

        (1)

        HttpServletRequest request = (HttpServletRequest)req;

        HttpServletResponse response = (HttpServletResponse)res;

        (2)

        request.setCharacterEncoding(Encoding);

        (3)

        String target = request.getRequestURI();

        if (contextPathLength != 0)

            target = target.substring(contextPathLength);

        (4)

        boolean[] isHandled = {false};

        try {

            handler.handle(target, request, response, isHandled);

         }

        catch (Exception e) {

            if (log.isErrorEnabled()) {

                String qs = request.getQueryString();

                log.error(qs == null ? target : target + "?" + qs, e);

            }

        }

        if (isHandled[0] == false)

            chain.doFilter(request, response);

            ->該方法會讓攔截器進入下一個方法。

方法:destroy()

->生命週期完結。

完成 JFinalConfig 中對項目的一系列加載。

相關文章
相關標籤/搜索