1
我想在action類的execute方法之前和之後做一些說日誌記錄的事情。我正在使用struts 1.3。這更像是方面編程。如何在Struts 1.3的動作執行方法之前和之後做些事情?
我試着通過覆蓋它來嘗試requestProcessor的processPreprocess(),但僅在execute()之前調用它。另外我想在兩個地方(之前和之後)訪問ActionMapping。
我該如何做到這一點?
我想在action類的execute方法之前和之後做一些說日誌記錄的事情。我正在使用struts 1.3。這更像是方面編程。如何在Struts 1.3的動作執行方法之前和之後做些事情?
我試着通過覆蓋它來嘗試requestProcessor的processPreprocess(),但僅在execute()之前調用它。另外我想在兩個地方(之前和之後)訪問ActionMapping。
我該如何做到這一點?
我想你應該嘗試過濾器來實現你的要求。在web.xml
中創建一個Filter做映射,並覆蓋doFilter
方法,如下面的代碼。
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
beforeMethod(request,response);
chain.doFilter(request, response);
afterMethod(request,response);
}
如果過濾器不適用或適合您的要求請嘗試下面的邏輯。
org.apache.struts.action.Action
創建一個Action類,例如MyAction
。MyAction
在您的Web應用程序中創建所有其他Action classes
。在MyAction
,創建一個方法operate()
,如在
public abstract ActionForward operate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;
在MyAction添加一個或多個通用方法來應用,例如before()
,after()
。
如果所有Action類都必須實現此方法,則使其成爲abstract
。
如果某些Action類將提供特定於案例的實現,請聲明受保護的方法併爲其指定默認實現。
在MyAction覆蓋執行梅索德如下面的代碼
public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
before();
ActionForward forward = operate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
after();
return forward ;
}
我想到這一點,但我需要訪問之前和之後的ActionMapping。我們可以通過getParamter(「Globals.MAPPING_KEY」)從請求中獲得它,但僅在RequestProcessor中添加它。在過濾器這不可用。 – Nils 2013-03-07 09:29:22
@Nils添加了另一個邏輯 – 2013-03-07 10:26:23
如果您對理解上面的代碼有任何疑問,請在此處評論。 – 2013-03-07 10:26:51