日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts自定义拦截器拦截器工作原理

發布時間:2024/8/26 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts自定义拦截器拦截器工作原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

0.攔截器的調用原理:

  攔截器是一個繼承了序列化接口的普通接口。其工作原理是講需要被攔截的對象作為參數傳到intercept()方法內,在方法內部對此對象進行處理之后再執行原方法。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. 定義一個攔截器的類


??? > 可以實現 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 //初始化方法,獲得數據庫信息。調用服務 17 } 18 19 @Override 20 public String intercept(ActionInvocation arg0) throws Exception { 21 22 //如果用戶登錄了就會有user屬性(Session里面存一個session屬性) 23 Object user = arg0.getInvocationContext().getSession().get("user"); 24 // 上面可能是系統權限的管理 25 String result=""; 26 System.out.println(user); 27 if(user!=null){ 28 // 當滿足條件時,讓其執行,相當于是放行 29 arg0.invoke(); 30 }else { 31 result = "logon"; 32 ActionContext.getContext().put("msg", "您沒有權限管理"); 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", "成功執行了addUI操作"); 9 System.out.println("執行addui操作"); 10 return "success"; 11 } 12 13 public String update(){ 14 ActionContext.getContext().put("msg", "成功執行了update操作"); 15 System.out.println("執行update操作"); 16 return "success"; 17 } 18 }

?

3.JSP頁面(主頁面,也就是開始時要訪問Action的頁面)

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?? 也就是被攔截到沒有權限時要去的頁面

<%@ 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請求默認值:class:com.opensymphony.xwork2.ActionSupportmethod:execute result的name屬性:success 方法中返回success即可跳轉到結果對應的頁面--><!-- 第一個package命名空間 --><package name="intercept" namespace="/" extends="struts-default"><!-- 攔截器 --><interceptors><!-- 定義剛才的攔截器 --><interceptor name="permission" class="Action.PrivelegeInterceptor"></interceptor><!-- 定義攔截器棧 --><interceptor-stack name="permissionStack"><!-- 攔截器棧里面可以引用另外一個攔截器,也可以引用另外一個攔截器棧 --><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中使用新定義的攔截器與默認的攔截器)

?

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. 注意: 在自定義的攔截器中可以選擇不調用 ActionInvocation 的 invoke() 方法. 那么后續的攔截器和 Action 方法將不會被調用.Struts 會渲染自定義攔截器 intercept 方法返回值對應的 result

這種經常用于權限管理,例如上面的攔截器,沒有權限時,返回的是logon,自動渲染返回值為設置的值,然后根據返回值在Action配置中跳轉到響應的頁面。

?

?

IV.測試

1.沒權限的測試,即session中不存在user

?

?

?

2.有權限的測試,即session中存在user

?

結果:

?

?

?

?

?

Struts2 攔截器 和 過濾器 的區別:

①、過濾器依賴于 Servlet 容器,而攔截器不依賴于 Servlet 容器。

②、Struts2 攔截器只能對 Action 請求起作用,而過濾器則可以對幾乎所 有請求起作用。

③、攔截器可以訪問 Action 上下文(ActionContext)、值棧里的對象 (ValueStack),而過濾器不能.

④、在 Action 的生命周期中,攔截器可以多次調用,而過濾器只能在容器 初始化時被調用一次。

?

轉載于:https://www.cnblogs.com/qlqwjy/p/7190784.html

總結

以上是生活随笔為你收集整理的Struts自定义拦截器拦截器工作原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。