struts面試題及答案【重要】

1. 簡述 Struts2 的工做流程: json

①. 請求發送給 StrutsPrepareAndExecuteFiltersession

②. StrutsPrepareAndExecuteFilter 斷定該請求是不是一個 Struts2 請 求(ActionMapping判斷),不是就放行。架構

  (根據路徑的後綴是 .action或者.doj進行判斷)app

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

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

        try {
            if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
                chain.doFilter(request, response);
            } else {
                prepare.setEncodingAndLocale(request, response);
                prepare.createActionContext(request, response);
                prepare.assignDispatcherToThread();
                request = prepare.wrapRequest(request);
                ActionMapping mapping = prepare.findActionMapping(request, response, true);
                if (mapping == null) {
                    boolean handled = execute.executeStaticResourceRequest(request, response);
                    if (!handled) {
                        chain.doFilter(request, response);
                    }
                } else {
                    execute.executeAction(request, response, mapping);
                }
            }
        } finally {
            prepare.cleanupRequest(request);
        }
    }

 

③. 若該請求是一個 Struts2 請求,則 StrutsPrepareAndExecuteFilter 把請求的處理交給 ActionProxy框架

    public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
            throws ServletException {

        Map<String, Object> extraContext = createContextMap(request, response, mapping);

        // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
        ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
        boolean nullStack = stack == null;
        if (nullStack) {
            ActionContext ctx = ActionContext.getContext();
            if (ctx != null) {
                stack = ctx.getValueStack();
            }
        }
        if (stack != null) {
            extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
        }

        String timerKey = "Handling request from Dispatcher";
        try {
            UtilTimerStack.push(timerKey);
            String namespace = mapping.getNamespace();
            String name = mapping.getName();
            String method = mapping.getMethod();

            ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                    namespace, name, method, extraContext, true, false);

            request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());

            // if the ActionMapping says to go straight to a result, do it!
            if (mapping.getResult() != null) {
                Result result = mapping.getResult();
                result.execute(proxy.getInvocation());
            } else {
                proxy.execute();
            }

            // If there was a previous value stack then set it back onto the request
            if (!nullStack) {
                request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
            }
        } catch (ConfigurationException e) {
            logConfigurationException(request, e);
            sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);
        } catch (Exception e) {
            if (handleException || devMode) {
                sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
            } else {
                throw new ServletException(e);
            }
        } finally {
            UtilTimerStack.pop(timerKey);
        }
    }

 

④. ActionProxy 建立一個 ActionInvocation 的實例,並進行初始化spa

    public ActionInvocation getInvocation() {
        return invocation;
    }

 

⑤. ActionInvocation 實例在調用 Action 的過程先後,涉及到相關攔截 器(Intercepter)的調用。code

⑥. Action 執行完畢,ActionInvocation 負責根據 struts.xml 中的配置 找到對應的返回結果。調用結果的 execute 方法,渲染結果。xml

⑦. 執行各個攔截器 invocation.invoke() 以後的代碼對象

⑧. 把結果發送到客戶端blog

 

2. Struts2 攔截器 和 過濾器 的區別:

①、過濾器依賴於 Servlet 容器,而攔截器不依賴於 Servlet 容器。

②、Struts2 攔截器只能對 Action 請求起做用,而過濾器則能夠對幾乎所 有請求起做用。

③、攔截器能夠訪問 Action 上下文(ActionContext)、值棧裏的對象 (ValueStack),而過濾器不能.

④、在 Action 的生命週期中,攔截器能夠屢次調用,而過濾器只能在容器 初始化時被調用一次。

 

3. 爲何要使用 Struts2 & Struts2 的優勢:

①. 基於 MVC 架構,框架結構清晰。

②. 使用 OGNL: OGNL 能夠快捷的訪問值棧中的數據、調用值棧中對象的方 法

③. 攔截器: Struts2 的攔截器是一個 Action 級別的 AOP, Struts2 中的 許多特性都是經過攔截器來實現的, 例如異常處理,文件上傳,驗證等。攔截器 是可配置與重用的

④. 多種表現層技術. 如:JSP、FreeMarker、Velocity 等

 

4. Struts2 如何訪問 HttpServletRequest、HttpSession、ServletContext 三個域對象 ?

①. 與 Servlet API 解耦的訪問方式

  > 經過 ActionContext 訪問域對象對應的 Map 對象

  > 經過實現 Aware 接口使 Struts2 注入對應的 Map 對象

②. 與 Servlet API 耦合的訪問方式

  > 經過 ServletActionContext 直接獲取 Servlet API 對象

  > 經過實現 ServletXxxAware 接口的方式使 Struts2 注入對應的對象

 

5. Struts2 中的默認包 struts-default 有什麼做用?

①. struts-default 包是 struts2 內置的,它定義了 struts2 內部的衆 多攔截器和 Result 類型,而 Struts2 不少核心的功能都是經過這些內置的攔 截器實現,如:從請求中把請求參數封裝到 action、文件上傳和數據驗證等等 都是經過攔截器實現的。當包繼承了 struts-default 包才能使用 struts2 爲我 們提供的這些功能。

② .struts-default 包 是 在 struts-default.xml 中 定 義 , struts-default.xml 也是 Struts2 默認配置文件。 Struts2 每次都會自動加 載 struts-default.xml 文件。

③. 一般每一個包都應該繼承 struts-default 包。

 

6. 說出 struts2 中至少 5 個的默認攔截器

exception;fileUpload;i18n;modelDriven;params;prepare;token; tokenSession;validation 等

 

7. 談談 ValueStack:

①. ValueStack 貫穿整個 Action 的生命週期,保存在 request 域中,所 以 ValueStack 和 request 的生命週期同樣. 當 Struts2 接受一個請求時,會 迅速建立 ActionContext,ValueStack,Action. 而後把 Action 存放進 ValueStack,因此 Action 的實例變量能夠被 OGNL 訪問。 請求來的時候, Action、ValueStack 的生命開始;請求結束,Action、ValueStack 的生命結束

②. 值棧是多實例的,由於 Action 是多例的(和 Servlet 不同,Servelt 是單例的),而每一個 Action 都有一個對應的值棧,Action 對象默認保存在棧頂;

③. ValueStack 本質上就是一個 ArrayList(查看源代碼獲得);

④. 使用 OGNL 訪問值棧的內容時,不須要#號,而訪問 request、session、 application、attr 時,須要加#號;

⑤. Struts2 重寫了 request 的 getAttribute 方法,因此可使用 EL 直接訪問值棧中的內容

 

8. ActionContext、ServletContext、pageContext 的區別 ?

①. ActionContext Struts2 的 API:是當前的 Action 的上下文環境

②. ServletContext 和 PageContext 是 Servlet 的 API

 

9. Struts2 有哪幾種結果類型 ?

參看 struts-default.xml 中的相關配置:dispatcher(轉發)、chain(轉發到Action)、redirect(重定向)、redirectAction(重定向到Action)、json 等.

 

10. 攔截器的生命週期與工做過程 ?

每一個攔截器都是須要實現 Interceptor 接口

> init():在攔截器被建立後當即被調用, 它在攔截器的生命週期內只 被調用一次. 能夠在該方法中對相關資源進行必要的初始化;

> intercept(ActionInvocation invocation):每攔截一個動做請求, 該方法就會被調用一次;

> destroy:該方法將在攔截器被銷燬以前被調用, 它在攔截器的生命周 期內也只被調用一次;

 

11. 如何在 Struts2 中使用 Ajax 功能 ?

①. JSON plugin

②. DOJO plugin

③. DWR plugin

④. 使用 Stream 結果類型.

相關文章
相關標籤/搜索