Struts2攔截器 關(guān)于解決登錄的問題

攔截器的工作原理如圖 攔截器是由每一個action請求(request)都包裝在一系列的攔截器的內(nèi)部,通過redirectAction再一次發(fā)送請求。
攔截器可以在Action執(zhí)行直線做相似的操作也可以在Action執(zhí)行直后做回收操作。
我們可以讓每一個Action既可以將操作轉(zhuǎn)交給下面的攔截器,Action也可以直接退出操作返回客戶既定的畫面。
接下來我們該如何定義一個攔截器:
自定義一個攔截器如下:
1、實現(xiàn)Interceptor接口或者繼承AbstractInterceptor抽象類。
2、創(chuàng)建一個Struts.xml文件進(jìn)行定義攔截器。
3、在需要使用的Action中引用上述定義的攔截器,為了方便也可將攔截器定義為默認(rèn)的攔截器(<default-interceptor-ref name="myStack"/>),
這樣在不加特殊聲明的情況下所有的Action都被這個攔截器攔截<param name="excludeMethods">loginView,login</param>。
①Interceptor接口聲明三個方法:
public class LoginInterceptor implements Interceptor {
private Map<String,Object> session = null;
public void destroy() { }
public void init() { }
public String intercept(ActionInvocation actionInvocation) throws Exception {
8 Object myAction = actionInvocation.getAction();
if(myAction instanceof UserAction){
System.out.println("你訪問的Action是UserAction,不要校驗Session,否則死循環(huán)");
//放行
return actionInvocation.invoke();
}else{
System.out.println("你訪問的Action是:"+myAction);
}
session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
}
}
注:該方法可以不加:<param name="excludeMethods">loginView,login</param>
②讓它繼承 MethodFilterInterceptor:
public class LoginInterceptor extends MethodFilterInterceptor {
private Map<String,Object> session = null;
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
/*
Object myAction = actionInvocation.getAction();
if(myAction instanceof UserAction){
System.out.println("你訪問的Action是UserAction,不要校驗Session,否則死循環(huán)");
//放行
return actionInvocation.invoke();
}else{
System.out.println("你訪問的Action是:"+myAction);
}
*/
session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
}
}
}
③UserAction繼承ActionSupport 實現(xiàn) ModelDriven<User>和SessionAware:
public class UserAction extends ActionSupport implements ModelDriven<User>,SessionAware{
private Map<String,Object> session = null;
private User user = null;
//驅(qū)動模型
public User getModel() {
this.user = new User();
return this.user;
}
public void setSession(Map<String, Object> map) {
this.session = map;
}
public String loginView(){
return "loginViewSuccess";
}
public String login(){
if ("admin".equals(user.getUserName())&&"123456".equals(user.getUserPassword())){
session.put("user",user);
return this.SUCCESS;
}else{
return this.ERROR;
}
}
}
Struts.xml文件中:
<struts>
<package name="myPackage" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor">
<!--excludeMethods需要生效的話,自定義的攔截器,不能使用實現(xiàn)Interceptor接口,而是extends MethodFilterInterceptor-->
<param name="excludeMethods">loginView,login</param><!--不用此行時 我們可以配合①使用攔截器-->
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--配置一個默認(rèn)攔截器,也就是所有的Action都必須使用-->
<default-interceptor-ref name="myStack"/>
<global-results>
<result name="login" type="redirectAction">userAction_loginView</result>
</global-results>
<!--不寫method,默認(rèn)就是execute-->
<action name="indexAction" class="com.nf.action.IndexAction" method="execute">
<result name="success">/WEB-INF/jsp/index.jsp</result>
<!--
<interceptor-ref name="myStack"></interceptor-ref>
-->
<!--注釋這里也可以放該代碼 只不過每一個action都要放比較麻煩
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action>
<action name="otherFunctionAction" class="com.nf.action.OtherFunctionAction">
<!--不寫name,默認(rèn)就是success-->
<result>/WEB-INF/jsp/otherFunction.jsp</result>
</action>
<action name="userAction_*" class="com.nf.action.UserAction" method="{1}">
<result name="loginViewSuccess">/WEB-INF/jsp/loginView.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
<result name="success" type="redirectAction">indexAction</result>
<allowed-methods>login,loginView</allowed-methods>
</action>
</package>
</struts>
其中,<param name="excludeMethods">loginView,login</param> 配置的過濾方法,意思是攔截器對其中的方法不起作用。在我這里,loginView是跳轉(zhuǎn)到登錄頁面的方法。
login 是驗證用戶名和密碼的方法,在其中會將通過驗證的用戶名放入session中。
總結(jié):
1.在struts2 中,所有的攔截器都會繼承 Interceptor 這個接口。
2.如果我們沒有添加攔截器,struts2 會為我們添加默認(rèn)攔截器。當(dāng)然我們要是指定了攔截器,我們自己的攔截器就會取代默認(rèn)的攔截器,
那么我們就不能享受默認(rèn)攔截器提供的一些功能。所以,一般我會把默認(rèn)攔截器也加上。
例如,在以上配置項中,action 里面再加上<interceptor-ref name="defaultStack"></interceptor-ref>
以上這篇Struts2攔截器 關(guān)于解決登錄的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
bug解決Failed_to_execute_goal_org.springframework
這篇文章主要為大家介紹了bug解決Failed_to_execute_goal_org.springframework,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
SpringBoot之@Value獲取application.properties配置無效的解決
這篇文章主要介紹了SpringBoot之@Value獲取application.properties配置無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Mybatis框架之代理模式(Proxy Pattern)的實現(xiàn)
本文主要介紹了MyBatis框架中使用代理模式ProxyPattern的原理和實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
Netty分布式ByteBuf使用SocketChannel讀取數(shù)據(jù)過程剖析
這篇文章主要為大家介紹了Netty源碼分析ByteBuf使用SocketChannel讀取數(shù)據(jù)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

