package com.zhang.controller.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//注意當(dāng)前類必須受Spring容器控制
@Component//定義攔截器類,實(shí)現(xiàn)HandlerInterceptor接口
public class ProjectInterceptor implements HandlerInterceptor { //原始方法調(diào)用前執(zhí)行的內(nèi)容
//返回值類型可以攔截控制的執(zhí)行,true放行,false終止
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("Controller執(zhí)行前面");
return true;
}
//原始方法調(diào)用后執(zhí)行的內(nèi)容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("Controller執(zhí)行后面");
}
//原始方法調(diào)用完成后執(zhí)行的內(nèi)容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("Controller執(zhí)行完成后");
}
}
3.2 配置加載攔截器,定義一個(gè)類繼承 WebMvcConfigurationSupport這個(gè)類注意要被SpringMvcConfig配置類掃描到,不然不生效
package com.zhang.config;
import com.zhang.controller.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MvcSupportConfig extends WebMvcConfigurationSupport {@Autowired
private ProjectInterceptor projectInterceptor;
protected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(projectInterceptor).addPathPatterns("/**");//攔截所有請(qǐng)求
}
}
3.3 Controllerpackage com.zhang.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {@GetMapping()
public String save(){System.out.println("正在執(zhí)行Controller:"+"Book...Save");
return "Hello SpringMvc";
}
}
3.4 posatman發(fā)送請(qǐng)求,后臺(tái)打印結(jié)果4. 攔截器流程分析5. 攔截器參數(shù)
5.1 前置處理//原始方法調(diào)用前執(zhí)行的內(nèi)容
//返回值類型可以攔截控制的執(zhí)行,true放行,false終止
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle..."+contentType);
return true;
}
參數(shù)
返回值
返回值為false,后面的Controller方法不執(zhí)行
//原始方法調(diào)用后執(zhí)行的內(nèi)容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...");
}
5.3 完成后處理注意:如果處理器方法出現(xiàn)異常了,該方法不會(huì)執(zhí)行
//原始方法調(diào)用完成后執(zhí)行的內(nèi)容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...");
}
參數(shù)
ex:如果處理器執(zhí)行過程中出現(xiàn)異常對(duì)象,可以針對(duì)異常情況進(jìn)行單獨(dú)處理
6. 攔截器鏈配置 6.1 多個(gè)攔截器配置注意:無論處理器方法內(nèi)部是否出現(xiàn)異常,該方法都會(huì)執(zhí)行。
定義第二個(gè)攔截器
@Component
public class ProjectInterceptor2 implements HandlerInterceptor {@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle...222");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...222");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...222");
}
}
配置第二個(gè)攔截器
package com.zhang.config;
import com.zhang.controller.interceptor.ProjectInterceptor;
import com.zhang.controller.interceptor.ProjectInterceptor2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MvcSupportConfig extends WebMvcConfigurationSupport {@Autowired
private ProjectInterceptor2 projectInterceptor2;
@Autowired
private ProjectInterceptor projectInterceptor;
protected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(projectInterceptor).addPathPatterns("/**");//攔截所有請(qǐng)求
registry.addInterceptor(projectInterceptor2).addPathPatterns("/**");
}
}
6.2 多個(gè)連接器工作流程分析你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
分享題目:9.SpringMvc攔截器-創(chuàng)新互聯(lián)
瀏覽地址:http://jinyejixie.com/article38/dedipp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、動(dòng)態(tài)網(wǎng)站、App設(shè)計(jì)、微信公眾號(hào)、營銷型網(wǎng)站建設(shè)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)