Struts2學習筆記(十一):自訂攔截器

來源:互聯網
上載者:User

要自訂攔截器需要實現com.opensymphony.xwork2.interceptor.Interceptor介面:

public class PermissionInterceptor implements Interceptor {
private static final long serialVersionUID = -5178310397732210602L;
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("進入攔截器");
if(session裡存在使用者){
String result = invocation.invoke();
}else{
return “logon”;
}
//System.out.println("傳回值:"+ result);
//return result;
}
}

 

<package name="jim" namespace="/test" extends="struts-default">
<interceptors>
<interceptor name=“permission" class="com.jim.aop.PermissionInterceptor" />
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name=" permission " />
</interceptor-stack>
</interceptors>
<action name="helloworld_*" class="com.jim.action.HelloWorldAction" method="{1}">
<result name="success">/WEB-INF/page/hello.jsp</result>
<interceptor-ref name="permissionStack"/>
</action>
</package>

 

因為struts2中如檔案上傳,資料驗證,封裝請求參數到action等功能都是由系統預設的defaultStack中的攔截器實現的,所以我們定義的攔截器需要引用系統預設的defaultStack,這樣應用才可以使用struts2架構提供的眾多功能。

如果希望包下的所有action都使用自訂的攔截器,可以通過<default-interceptor-ref name=“permissionStack”/>把攔截器定義為預設攔截器。注意:每個包只能指定一個預設攔截器。另外,一旦我們為該包中的某個action顯式指定了某個攔截器,則預設攔截器不會起作用。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.