您好,登錄后才能下訂單哦!
這篇文章主要講解了“java攔截器的作用是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“java攔截器的作用是什么”吧!
1、攔截器類(lèi):IndexInterceptor
package interceptor; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class IndexInterceptor extends HandlerInterceptorAdapter { /** * 在業(yè)務(wù)處理器處理請(qǐng)求之前被調(diào)用 * 如果返回false * 從當(dāng)前的攔截器往回執(zhí)行所有攔截器的afterCompletion(),再退出攔截器鏈 * 如果返回true * 執(zhí)行下一個(gè)攔截器,直到所有的攔截器都執(zhí)行完畢 * 再執(zhí)行被攔截的Controller * 然后進(jìn)入攔截器鏈, * 從最后一個(gè)攔截器往回執(zhí)行所有的postHandle() * 接著再?gòu)淖詈笠粋€(gè)攔截器往回執(zhí)行所有的afterCompletion() */ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle(), 在訪問(wèn)Controller之前被調(diào)用"); return true; } /** * 在業(yè)務(wù)處理器處理請(qǐng)求執(zhí)行完成后,生成視圖之前執(zhí)行的動(dòng)作 * 可在modelAndView中加入數(shù)據(jù),比如當(dāng)前時(shí)間 */ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle(), 在訪問(wèn)Controller之后,訪問(wèn)視圖之前被調(diào)用,這里可以注入一個(gè)時(shí)間到modelAndView中,用于后續(xù)視圖顯示"); modelAndView.addObject("date","由攔截器生成的時(shí)間:" + new Date()); } /** * 在DispatcherServlet完全處理完請(qǐng)求后被調(diào)用,可用于清理資源等 * * 當(dāng)有攔截器拋出異常時(shí),會(huì)從當(dāng)前攔截器往回執(zhí)行所有的攔截器的afterCompletion() */ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion(), 在訪問(wèn)視圖之后被調(diào)用"); } }
2、配置攔截器
修改springmvc-servlet.xml以對(duì)/index路徑進(jìn)行攔截
如果要攔截其他路徑:
/** 攔截所有
/category/** 攔截/category路徑下的所有
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index">indexController</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"></bean> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/index"/> <!-- 定義在mvc:interceptor下面的表示是對(duì)特定的請(qǐng)求才進(jìn)行攔截的 --> <bean class="interceptor.IndexInterceptor"/> </mvc:interceptor> <!-- 當(dāng)設(shè)置多個(gè)攔截器時(shí),先按順序調(diào)用preHandle方法,然后逆序調(diào)用每個(gè)攔截器的postHandle和afterCompletion方法 --> </mvc:interceptors> </beans>
3、修改 index.jsp,打印攔截器放進(jìn)去的日期
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <h2>${message}</h2> <p>${date}</p>
感謝各位的閱讀,以上就是“java攔截器的作用是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)java攔截器的作用是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。