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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

strust2自定义拦截器

發(fā)布時間:2023/11/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 strust2自定义拦截器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.創(chuàng)建一個攔截器類,繼承MethodFilterInterceptor類,實現(xiàn)doIntercept方法

package com.yqg.bos.web.interceptor;import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import com.yqg.bos.entity.User; import com.yqg.bos.utils.CommonUtils;public class BosInterceptor extends MethodFilterInterceptor{//該攔截器功能為如果session中的用戶為空,跳轉(zhuǎn)到登錄界面。protected String doIntercept(ActionInvocation invocation) throws Exception {User user = CommonUtils.getLoginUser();if(user == null) {
//如果user為空,跳回login.jsp界面,否則放行
return "login";}else {return invocation.invoke();}}}

package com.yqg.bos.utils;import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;import com.yqg.bos.entity.User;public class CommonUtils {public static HttpSession getSession() {return ServletActionContext.getRequest().getSession();}public static User getLoginUser() {return (User) getSession().getAttribute("loginUser");} }

2.在struts.xml中配置該攔截器,并設置一個自定義攔截器棧作為默認攔截器棧。

<interceptors>
<!--將自定義的攔截器注冊到struts中--> <interceptor name="bosInterceptor" class="com.yqg.bos.web.interceptor.BosInterceptor">
<!--除login方法之外的所有方法都被攔截--><param name = "excludeMethods">login</param></interceptor>
<!--自定義攔截器棧,并將自定義攔截器加入到棧中,還將struts默認的攔截器棧加入到自定義攔截器棧中--><interceptor-stack name="myStack"><interceptor-ref name="bosInterceptor"></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors>
<!--設置自定義攔截器棧為默認攔截器棧取代struts的默認攔截器棧defaultStack--><default-interceptor-ref name="myStack" />
<!--定義攔截結果--><global-results><result name="login">/login.jsp</result></global-results>

?

轉(zhuǎn)載于:https://www.cnblogs.com/yanqingguo/p/9827158.html

總結

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。