strust2自定义拦截器
生活随笔
收集整理的這篇文章主要介紹了
strust2自定义拦截器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.創建一個攔截器類,繼承MethodFilterInterceptor類,實現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中的用戶為空,跳轉到登錄界面。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>
?
轉載于:https://www.cnblogs.com/yanqingguo/p/9827158.html
總結
以上是生活随笔為你收集整理的strust2自定义拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 布衣天下剧情介绍
- 下一篇: 元气骑士手游红爵士怎么遇到