struts2學習(13)——攔截器的配置

今天學習了struts2的攔截器功能,我們都知道struts2是基於weblogic和Filter攔截器爲基礎的。

struts2本身爲我們提供了大量的攔截器,例如當我們設置的屬性,request,application中的屬性,這些屬性的添加都是在攔截器的作用下完成的。

struts2的defaultStack中定義了框架本身自帶的攔截器,我們可以通過打開struts-core-2.23.2(版本可能不同),在其中就有struts-default.xml文件,中間定義了struts的默認攔截器。


打開文件我們可以看到:

 <interceptors>
            <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
            <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
            <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
            <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
            <interceptor name="cookieProvider" class="org.apache.struts2.interceptor.CookieProviderInterceptor"/>
            <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
            <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
            <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
            <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
            <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
            <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
            <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
            <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
            <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
            <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
            <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
            <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
            <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
            <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
            <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
            <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
            <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
            <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
            <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
            <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
            <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
            <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
            <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
            <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
            <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
            <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
            <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />

這就是struts給我們已經定義的interceptor,幫我們完成了我們大部分的功能。

當然在struts中自定義一個攔截器也是相當的簡單的。

首先我們需要創建一個類,讓它來完成攔截功能

import com.opensymphony.xwork2.interceptor.Interceptor;

public class permission implements Interceptor{

	public void destroy() {
	
	}

	public void init() {

	}

	public String intercept(ActionInvocation invocation) throws Exception {
		Object user = ActionContext.getContext().getSession().get("user");
		if(user != null){
			System.out.println("用戶已經登錄,放行");
		 	 return invocation.invoke();//說明用戶已經登錄,放行
		}else{
			ActionContext.getContext().put("message", "用戶尚未登錄,請先登錄!");
			System.out.println("用戶尚未登錄,請先登錄");
			return "message";
		}
	}

}

注意一個攔截器一定要實現import com.opensymphony.xwork2.interceptor.Interceptor

然後我們要註冊這個攔截器

<interceptors>
   			<interceptor name="permission" class="cn.itcast.Interceptor.permission"></interceptor>
   			<interceptor-stack name="permissionStack">
   				<interceptor-ref name="defaultStack"></interceptor-ref>
   				<interceptor-ref name="permission"></interceptor-ref>
   			</interceptor-stack>
   		</interceptors>


在這裏我們
<interceptor name="permission" class="cn.itcast.Interceptor.permission"></interceptor>
註冊了這個攔截器
之後我們有定義了一個攔截器棧,在這個攔截器棧中我們定義了struts2的默認攔截器和我們自定義的攔截器,這是因爲當我們給一個action綁定攔截器的時候,倘若我們只將我們自定義的攔截器綁定上,那麼這個action將失去struts2默認的攔截器功能,這是不可想象的。所以當我們給我們的action綁定攔截器的時候一定要加上defaultStack,這樣既可以保證自定義的攔截器可以使用,也不會影響默認攔截器。


<action name="hello_*" class="cn.itcast.action.helloAction" method="{1}">
   			<result name="success">/WEB-INF/page/hello.jsp</result>
   			<interceptor-ref name="permissionStack"></interceptor-ref>
   		</action>

我們在這裏綁定了攔截器。

這樣我們的自定義攔截器就ok了。














發佈了55 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章