如何在攔截器中獲取url路徑里面@PathVariable的參數(shù)值
在攔截器中獲取url路徑里@PathVariable參數(shù)值
解決辦法
Map pathVariables = (Map) request.getAttribute( HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); String classId = (String)pathVariables.get("classId");
示例接口
// 獲取某個(gè)班級(jí)下面的學(xué)生列表 @RequestMapping("/classes/{classId}/students") public String list(@PathVariable String classId){ return "學(xué)生列表"; }
完整示例
package com.example.demo; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ModelAndView; import java.util.Map; public class SpringMVCInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); String chatbotId = (String)pathVariables.get("classId"); System.out.println("classId: " + classId); if ("1234".equals(classId)) return true; return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } }
package com.example.demo; 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 MvcInterceptorConfig extends WebMvcConfigurationSupport{ @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SpringMVCInterceptor()).addPathPatterns("/chatbot/**"); super.addInterceptors(registry); } }
spring @PathVariable:請(qǐng)求路徑url 上有變量值,通過@PathVariable獲取
請(qǐng)求路徑url 上有個(gè)變量值,可以通過@PathVariable來獲取
示例:
id為變量值:
@RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
Tool tool = new Tool(); tool.setUrls(new String[]{"finance/account/loanDownExcel/cur","finance/account/loanDownExcel/all"});
@RequestMapping("accountlog/loanDownExcel/{type}") public void exportAccountLogExcel(@PathVariable("type") String type)throws Exception {}
或者是
public void exportAccountLogExcel(@PathVariable String type)throws Exception {}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot靜態(tài)資源路徑的配置與修改詳解
最近在做SpringBoot項(xiàng)目的時(shí)候遇到了“白頁”問題,通過查資料對(duì)SpringBoot訪問靜態(tài)資源做了總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(4)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌
這篇文章主要介紹了python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04Hibernate Validator實(shí)現(xiàn)更簡(jiǎn)潔的參數(shù)校驗(yàn)及一個(gè)util
這篇文章主要介紹了Hibernate Validator實(shí)現(xiàn)更簡(jiǎn)潔的參數(shù)校驗(yàn)及一個(gè)util,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05Java Synchronize下的volatile關(guān)鍵字詳解
這篇文章主要介紹了Java Synchronize下的volatile關(guān)鍵字詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Java使用itext5實(shí)現(xiàn)PDF表格文檔導(dǎo)出
這篇文章主要介紹了Java使用itext5實(shí)現(xiàn)PDF表格文檔導(dǎo)出,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01springsecurity?登錄認(rèn)證流程分析一(ajax)
這篇文章主要介紹了springsecurity?登錄認(rèn)證一(ajax篇),本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08