1. 簡述 Struts2 的工作流程:
①. 請求傳送給 StrutsPrepareAndExecuteFilter
②. StrutsPrepareAndExecuteFilter 判定該請求是否是一個 Struts2 請 求(ActionMapping判斷),不是就放行。
(根據路徑的字尾是 .action或者.doj進行判斷)
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 的例項,並進行初始化
public ActionInvocation getInvocation() { return invocation; }
⑤. ActionInvocation 例項在呼叫 Action 的過程前後,涉及到相關攔截 器(Intercepter)的呼叫。
⑥. Action 執行完畢,ActionInvocation 負責根據 struts.xml 中的配置 找到對應的返回結果。呼叫結果的 execute 方法,渲染結果。
⑦. 執行各個攔截器 invocation.invoke() 之後的程式碼
⑧. 把結果傳送到客戶端
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 結果型別.