更新時(shí)間:2021-09-13 11:12:14 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1751次
HandlerInterceptor接口,實(shí)現(xiàn)接口內(nèi)的方法
public class LoginInterceptor implements HandlerInterceptor {
//執(zhí)行控制器之前的攔截程序
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if(user == null) {
String contextPath = request.getContextPath();
response.sendRedirect(contextPath+"/user/login");
return false;
}else {
return true;
}
}
//執(zhí)行控制器之后的邏輯
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
//視圖渲染之后的邏輯
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
spring-mvc配置文件內(nèi)容
<!-- 攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/static/**"/>
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/AjaxDoLogin"/>
<bean class="com.cn.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
繼承HandlerIntercrprorAdapter實(shí)現(xiàn)類并重寫preHandle方法
邏輯和上面的類似
以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"Spring攔截器的兩種實(shí)現(xiàn)方式",希望對(duì)大家有幫助,想了解更多可查看Spring框架教程。動(dòng)力節(jié)點(diǎn)在線學(xué)習(xí)教程,針對(duì)沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識(shí),讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743