Struts自定义拦截器拦截器工作原理
0.攔截器的調(diào)用原理:
攔截器是一個(gè)繼承了序列化接口的普通接口。其工作原理是講需要被攔截的對(duì)象作為參數(shù)傳到intercept()方法內(nèi),在方法內(nèi)部對(duì)此對(duì)象進(jìn)行處理之后再執(zhí)行原方法。intercept(ActionInvocation invocation)是攔截處理的方法。
?
Interceptor .java?
?
public interface Interceptor extends Serializable {/*** Called to let an interceptor clean up any resources it has allocated.*/void destroy();/*** Called after an interceptor is created, but before any requests are processed using* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving* the Interceptor a chance to initialize any needed resources.*/void init();/*** Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.** @param invocation the action invocation* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.*/String intercept(ActionInvocation invocation) throws Exception;}?
?
?
1.自定義攔截器步驟
I. 定義一個(gè)攔截器的類
??? > 可以實(shí)現(xiàn) Interceptor 接口
?? ?> 繼承 AbstractInterceptor 抽象類
?
1.攔截器
1 package Action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionInvocation; 5 import com.opensymphony.xwork2.interceptor.Interceptor; 6 7 public class PrivelegeInterceptor implements Interceptor { 8 9 @Override 10 public void destroy() { 11 // TODO Auto-generated method stub 12 } 13 @Override 14 public void init() { 15 // TODO Auto-generated method stub 16 //初始化方法,獲得數(shù)據(jù)庫(kù)信息。調(diào)用服務(wù) 17 } 18 19 @Override 20 public String intercept(ActionInvocation arg0) throws Exception { 21 22 //如果用戶登錄了就會(huì)有user屬性(Session里面存一個(gè)session屬性) 23 Object user = arg0.getInvocationContext().getSession().get("user"); 24 // 上面可能是系統(tǒng)權(quán)限的管理 25 String result=""; 26 System.out.println(user); 27 if(user!=null){ 28 // 當(dāng)滿足條件時(shí),讓其執(zhí)行,相當(dāng)于是放行 29 arg0.invoke(); 30 }else { 31 result = "logon"; 32 ActionContext.getContext().put("msg", "您沒(méi)有權(quán)限管理"); 33 } 34 System.out.println(result); 35 return result; 36 } 37 }?
?
2.被攔截的Action
1 package Action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 5 public class UserAction { 6 7 public String addUI(){ 8 ActionContext.getContext().put("msg", "成功執(zhí)行了addUI操作"); 9 System.out.println("執(zhí)行addui操作"); 10 return "success"; 11 } 12 13 public String update(){ 14 ActionContext.getContext().put("msg", "成功執(zhí)行了update操作"); 15 System.out.println("執(zhí)行update操作"); 16 return "success"; 17 } 18 }?
3.JSP頁(yè)面(主頁(yè)面,也就是開(kāi)始時(shí)要訪問(wèn)Action的頁(yè)面)
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <%-- <% 11 session.setAttribute("user", "uuuu"); 12 %> --%> 13 Success 14 ${msg } 15 <a href="/struts2interceptor/user_addUI.action">addui</a> 16 <a href="/struts2interceptor/user_update.action">update</a> 17 </body> 18 </html>?
?
msg.jsp?? 也就是被攔截到?jīng)]有權(quán)限時(shí)要去的頁(yè)面
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> ${msg } </body> </html>?
?
?
?
II. 在 struts.xml 文件配置.?? ?
?
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- package的名字必須唯一!只能攔截.action請(qǐng)求默認(rèn)值:class:com.opensymphony.xwork2.ActionSupportmethod:execute result的name屬性:success 方法中返回success即可跳轉(zhuǎn)到結(jié)果對(duì)應(yīng)的頁(yè)面--><!-- 第一個(gè)package命名空間 --><package name="intercept" namespace="/" extends="struts-default"><!-- 攔截器 --><interceptors><!-- 定義剛才的攔截器 --><interceptor name="permission" class="Action.PrivelegeInterceptor"></interceptor><!-- 定義攔截器棧 --><interceptor-stack name="permissionStack"><!-- 攔截器棧里面可以引用另外一個(gè)攔截器,也可以引用另外一個(gè)攔截器棧 --><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="permission"></interceptor-ref></interceptor-stack></interceptors><action name="user_*" class="Action.UserAction" method="{1}"><result>/index.jsp</result> <result name="logon">/msg.jsp</result><!-- 使用攔截器棧 --><interceptor-ref name="permissionStack"></interceptor-ref></action></package></struts>
?
?
第二種使用攔截器的方式是:(在前面定義一下攔截器,然后在Action中使用新定義的攔截器與默認(rèn)的攔截器)
?
1 <interceptors> 2 3 <interceptor name="hello" class="com.atguigu.struts2.interceptors.MyInterceptor"></interceptor> 4 5 </interceptors> 6 7 <action name="testToken" class="com.atguigu.struts2.token.app.TokenAction"> 8 <interceptor-ref name="hello"></interceptor-ref> 9 <interceptor-ref name="defaultStack"></interceptor-ref> 10 <result>/success.jsp</result> 11 <result name="invalid.token">/token-error.jsp</result> 12 </action>?
?III. 注意: 在自定義的攔截器中可以選擇不調(diào)用 ActionInvocation 的 invoke() 方法. 那么后續(xù)的攔截器和 Action 方法將不會(huì)被調(diào)用.Struts 會(huì)渲染自定義攔截器 intercept 方法返回值對(duì)應(yīng)的 result
這種經(jīng)常用于權(quán)限管理,例如上面的攔截器,沒(méi)有權(quán)限時(shí),返回的是logon,自動(dòng)渲染返回值為設(shè)置的值,然后根據(jù)返回值在Action配置中跳轉(zhuǎn)到響應(yīng)的頁(yè)面。
?
?
IV.測(cè)試
1.沒(méi)權(quán)限的測(cè)試,即session中不存在user
?
?
?
2.有權(quán)限的測(cè)試,即session中存在user
?
結(jié)果:
?
?
?
?
?
Struts2 攔截器 和 過(guò)濾器 的區(qū)別:
①、過(guò)濾器依賴于 Servlet 容器,而攔截器不依賴于 Servlet 容器。
②、Struts2 攔截器只能對(duì) Action 請(qǐng)求起作用,而過(guò)濾器則可以對(duì)幾乎所 有請(qǐng)求起作用。
③、攔截器可以訪問(wèn) Action 上下文(ActionContext)、值棧里的對(duì)象 (ValueStack),而過(guò)濾器不能.
④、在 Action 的生命周期中,攔截器可以多次調(diào)用,而過(guò)濾器只能在容器 初始化時(shí)被調(diào)用一次。
?
轉(zhuǎn)載于:https://www.cnblogs.com/qlqwjy/p/7190784.html
總結(jié)
以上是生活随笔為你收集整理的Struts自定义拦截器拦截器工作原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: BEGINNING SHAREPOINT
- 下一篇: C++ TR1、TR2与boost的关系