攔截器: 用來對訪問的url進行攔截處理
用處: 權限驗證,亂碼設置等
spring-mvc.xml文件中的配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/tx ">
<!--編寫攔截器--> <mvc:interceptors> <!--對特定的url攔截--> <mvc:interceptor> <mvc:mapping path="/test.do"/> <bean class="com.hbut.interceptor.TestInterceptor"/> </mvc:interceptor> <mvc:interceptor> <!--對特定的模塊攔截第一級別攔截--> <mvc:mapping path="/test/×/"/> <bean class="com.hbut.interceptor.TestInterceptor1"/> </mvc:interceptor> <mvc:interceptor> <!--對特定的模塊攔截--> <mvc:mapping path="/test/×"/> <bean class="com.hbut.interceptor.TestInterceptor2"/> </mvc:interceptor> </mvc:interceptors>
對所有的url進行攔截
<mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>
java代碼
package com.hbut.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @Author XiJun.Gong * @DATE 2016/6/1. * aim: com.hbut.interceptor */public class TestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { //todo 在此處添加要操作code System.out.println("preHandle"); return true; //todo 此處為false時,請求不會到達control層 } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); //todo 可以用來修改信息,跳轉等 } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion"); //todo 最后執行 }}另一種攔截器:大同小異
package com.hbut.interceptor;import org.springframework.ui.ModelMap;import org.springframework.web.context.request.WebRequest;import org.springframework.web.context.request.WebRequestInterceptor;/** * @Author XiJun.Gong * @DATE 2016/6/1. * aim: com.hbut.interceptor */public class Test2Interceptor implements WebRequestInterceptor { @Override public void preHandle(WebRequest webRequest) throws Exception { } @Override public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception { } @Override public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { }}過濾器: 依賴于servlet容器,使用回調函數,過濾范圍大
攔截器: 依賴于框架容器 比如spring、mybatis ,靈活
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答