登入檢查攔截器 (ActionInvocation的理解)

來源:互聯網
上載者:User

1.actionInvocation是什麼

ActionInvocation就是Action的調用者。ActionInvocation在Action的執行過程中,負責Interceptor、Action和Result等一系列元素的調度。

理解ActionInvocation你需要對Action的調用過程有一個全面的瞭解:

http://tech.ddvip.com/2009-01/1232090919106049.html

講的還是很清楚地。

2.ActionInvocation幹什麼

Interceptor通過ActionInvocation可以完全的改變Action行為:不讓它執行、改變傳回值、甚至可以細顆粒的操作Action的方法

查看ActionINvocation 的API,他有getActionProxy(),getResultCode();getActionContext();

ActionProxy在action中用來構建執行環境,ActionProxy提供Action執行的時候一切所需要的配置、參數等等,當然,也要有進行Action調用的入口。


public String intercept(ActionInvocation invocation) throws Exception { Map session=invocation.getInvocationContext().getSession();    String  actionName=invocation.getProxy().getActionName();    String  methodName=invocation.getProxy().getMethod();}你寫過濾器的時候 有這個方法吧,如果沒有你繼承extends AbstractInterceptor

樣本:
public String intercept(ActionInvocation invocation) throws Exception{
ActionContext  ctx = invocation.getInvocationContext();
//如果請求的action在相應範圍記憶體在,則直接return出intercept方法,調用相應action去執行
for(String action:whiteList){
if(ctx.getName().equals(action)){
return invocation.invoke();
}
}
//如果請求的action不在定義的範圍內,則查看是否已經登入,是否有許可權做這個action的操作
HttpSession session = ServletActionContext.getRequest().getSession();
if(session.getAttribute("SESSION_ADMIN") == null){
session.setAttribute("sessionErrorMessage","請先登入系統。");
return "loginError";
}
//如果請求的action不在相應範圍內,但是已經登入,則直接執行該action請求
return invocation.invoke();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.