struts2自定義攔截器的示例代碼
題目:使用struts2自定義攔截器,完成用戶登陸才能訪問權(quán)限的實(shí)現(xiàn)
- 在session中存放user變量表示用戶登陸,若user為空則用戶沒有登陸,反之登陸
- 顯示提示信息(請(qǐng)先登錄)
定義攔截器
在struts.xml中定義攔截器使用標(biāo)簽<Intercaptors>、<Intercapter>。
<interceptors>
<interceptor name="test" class="Intercaptor.Intercaptor" />
<interceptor-stack name="testStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="test" />
</interceptor-stack>
</interceptors>
注:當(dāng)我們?yōu)槟硞€(gè)action添加Intercaptor時(shí)就會(huì)放棄struts2的其他的攔截器,所以我們要把自定義的攔截器放在一個(gè)一個(gè)攔截器棧中。
name屬性就是Intercaptor.Intercaptor類在服務(wù)器上的一個(gè)實(shí)例
class屬性就是這個(gè)攔截器的的類
實(shí)現(xiàn)攔截器
攔截器的java類要實(shí)現(xiàn)Intercaptor這個(gè)接口和里面的方法intercept()。我們這里攔截的條件是用戶是否登陸,也就是session中的user變量是否為空。
public class Intercaptor implements Interceptor{
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
Object user=ActionContext.getContext().getSession().get("user");
if(user!=null){
return invocation.invoke();
}
ActionContext.getContext().put("message", "請(qǐng)先登陸");
return "success";
}
}
實(shí)現(xiàn)業(yè)務(wù)邏輯
在action中添加攔截器
<action name="Action" class="Action.Action">
<interceptor-ref name="test"></interceptor-ref>
<result name="success">Message.jsp</result>
</action>
其他
action的實(shí)現(xiàn)
public class Action extends ActionSupport{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() throws Exception {
return "success";
}
}
index.jsp
<body>
用戶狀態(tài):${user!=null?"已登陸":"未登陸"}<br>
<a href="UserLogin.jsp" rel="external nofollow" >用戶登陸</a>
<a href="UserQuit.jsp" rel="external nofollow" >用戶退出</a>
<form action="<%request.getContextPath(); %>/testIntercaptor/Action">
<input type="submit" value="登陸后的操作">
</form>
</body>

UserLogin.jsp
在request.getSesssion中存放user變量
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
登陸成功
<%
request.getSession().setAttribute("user", "user");
response.setHeader("refresh", "1;url=index.jsp");
%>
UserQuit.jsp
移除request.getSesssion中user變量
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
退出成功
<%
request.getSession().removeAttribute("user");
response.setHeader("refresh", "1;url=index.jsp");
%>
Message.jsp
簡(jiǎn)單是輸出message和debug
<body>
${message } <br/>
<s:debug></s:debug>
</body>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Ribbon客戶端詳細(xì)介紹
Spring Cloud Ribbon 是一套基于 Netflix Ribbon 實(shí)現(xiàn)的客戶端負(fù)載均衡和服務(wù)調(diào)用工具。通過Spring Cloud的封裝,可以讓我們輕松地將面向服務(wù)的REST模版請(qǐng)求自動(dòng)轉(zhuǎn)換成客戶端負(fù)載均衡的服務(wù)調(diào)用2022-09-09
Java?Lambda表達(dá)式常用的函數(shù)式接口
這篇文章主要介紹了Java?Lambda表達(dá)式常用的函數(shù)式接口,文章基于Java?Lambda表達(dá)式展開對(duì)常用的函數(shù)式接口的介紹,具有一的的參考價(jià)值需要的小伙伴可以參考一下2022-04-04
Java @Value("${xxx}")取properties時(shí)中文亂碼的解決
這篇文章主要介紹了Java @Value("${xxx}")取properties時(shí)中文亂碼的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
spring boot(三)之Spring Boot中Redis的使用
這篇文章主要介紹了spring boot(三)之Spring Boot中Redis的使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05
詳解mybatis.generator配上最新的mysql 8.0.11的一些坑
這篇文章主要介紹了詳解mybatis.generator配上最新的mysql 8.0.11的一些坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Spring?Boot?3.1中整合Spring?Security和Keycloak的方法
本文介紹在最新的SpringBoot3.1版本之下,如何將Keycloak和Spring?Security一起跑起來,文中結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-06-06
JavaWeb之Servlet注冊(cè)頁面的實(shí)現(xiàn)示例
注冊(cè)頁面是很多網(wǎng)站都會(huì)是使用的到,本文主要介紹了JavaWeb之Servlet注冊(cè)頁面的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
通過 JdbcTemplate 直接執(zhí)行 SQL 語句,結(jié)合源碼動(dòng)態(tài)編譯即可方便實(shí)現(xiàn)動(dòng)態(tài)修改代碼邏輯的效果,這篇文章主要介紹了JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用,需要的朋友可以參考下2023-09-09
Java?數(shù)據(jù)交換?Json?和?異步請(qǐng)求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,采用鍵值對(duì)的形式來表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲(chǔ),這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請(qǐng)求Ajax,需要的朋友可以參考下2023-09-09

