Struts2攔截器是基于AOP思想實現的,而AOP的實現是基于動態代理。Struts2攔截器會在訪問某個Action之前或者之后進行攔截,并且Struts2攔截器是可插拔的;Struts2攔截器棧就是將攔截器按照順序連接在一起的鏈,當滿足攔截的要求時,則會按照實現聲明的順序依次執行攔截器。
1、Struts2自定義攔截器介紹
Struts2所有攔截器都必須實現Interceptor接口,Interceptor接口主要有3個方法:
AbstractInterceptor抽象類實現了Interceptor 接口。并為init()和destroy()提供了一個空白的實現,所以在實際開發中,自定義攔截器只需要繼承AbstractInterceptor類, 并且實現intercept(ActionInvocation invocation)方法就可以了。
2、Struts2自定義攔截器創建步驟
1).創建一個類實現Interceptor接口或繼承AbstractInterceptor類。 2).重寫intercept方法,這個方法是真正攔截操作,如果想要繼續向下訪問其它攔截器,必須在intercept方法中通過參數ActionInvocation調用invoke方法。 3).配置攔截器,需要在struts.xml文件中配置,分為兩種情況:
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 </interceptors> 5 <default-interceptor-ref name="login"></default-interceptor-ref> 6 <action name="loginAction" class="com.sunny.action.LoginAction"> 7 <result>/success.jsp</result> 8 <result name="error">/error.jsp</result> 9 </action>10 </package>
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 <interceptor-stack name="my"> 5 <interceptor-ref name="login"></interceptor-ref> 6 <interceptor-ref name="defaultStack"></interceptor-ref> 7 </interceptor-stack> 8 </interceptors> 9 <default-interceptor-ref name="my"></default-interceptor-ref>10 <action name="loginAction" class="com.sunny.action.LoginAction">11 <result>/success.jsp</result>12 <result name="error">/error.jsp</result>13 </action>14 </package>
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 </interceptors> 5 <action name="loginAction" class="com.sunny.action.LoginAction"> 6 <result>/success.jsp</result> 7 <result name="error">/error.jsp</result> 8 <interceptor-ref name="login"></interceptor-ref> 9 <interceptor-ref name="defaultStack"></interceptor-ref>10 </action>11 </package>
1 <package name="default" namespace="/" extends="struts-default"> 2 <interceptors> 3 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 4 <interceptor-stack name="my"> 5 <interceptor-ref name="login"></interceptor-ref> 6 <interceptor-ref name="defaultStack"></interceptor-ref> 7 </interceptor-stack> 8 </interceptors> 9 <action name="loginAction" class="com.sunny.action.LoginAction">10 <result>/success.jsp</result>11 <result name="error">/error.jsp</result>12 <interceptor-ref name="my"></interceptor-ref>13 </action>14 </package>
2、Struts2自定義攔截器實現示例:判斷是否登錄
該示例主要是用來驗證用戶是否登錄,如果沒登錄,就跳轉到error.jsp頁面,提示需要登錄系統。
攔截器類:
1 public class LoginIntercept extends AbstractInterceptor { 2 3 @Override 4 public String intercept(ActionInvocation invocation) throws Exception { 5 Map session = ServletActionContext.getContext().getSession(); 6 if (session.get("user")==null) { 7 return "error"; 8 } else { 9 return invocation.invoke();10 }11 }12 }
Action類:
1 public class LoginAction extends ActionSupport { 2 PRivate String name; 3 public String getName() { 4 return name; 5 } 6 public void setName(String name) { 7 this.name = name; 8 } 9 @Override10 public String execute() throws Exception {11 return "success";12 }13 }
struts.xml配置文件:
1 <struts> 2 <constant name="struts.devMode" value="true" /> 3 4 <package name="default" namespace="/" extends="struts-default"> 5 <interceptors> 6 <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor> 7 <interceptor-stack name="my"> 8 <interceptor-ref name="login"></interceptor-ref> 9 <interceptor-ref name="defaultStack"></interceptor-ref>10 </interceptor-stack>11 </interceptors>12 <action name="loginAction" class="com.sunny.action.LoginAction">13 <result>/success.jsp</result>14 <result name="error">/error.jsp</result>15 <interceptor-ref name="my"></interceptor-ref>16 </action>17 </package>18 </struts>
input.jsp頁面:
1 <body>2 <form action="${pageContext.servletContext.contextPath}/loginAction.action">3 姓名:<input type="text" name="name"><br>4 <input type="submit" value="提交">5 </form>6 </body>
error.jsp頁面:
1 <body>2 請登錄系統3 </body>
登錄界面:
由于沒有登錄系統,所以點擊提交之后,會顯示:
新聞熱點
疑難解答