如何在攔截器中獲取url路徑里面@PathVariable的參數(shù)值
更新時間:2021年08月23日 09:12:05 作者:nimo10050
這篇文章主要介紹了如何在攔截器中獲取url路徑里面@PathVariable的參數(shù)值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
在攔截器中獲取url路徑里@PathVariable參數(shù)值
解決辦法
Map pathVariables = (Map) request.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String classId = (String)pathVariables.get("classId");
示例接口
// 獲取某個班級下面的學(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:請求路徑url 上有變量值,通過@PathVariable獲取
請求路徑url 上有個變量值,可以通過@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 {}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java數(shù)據(jù)庫數(shù)據(jù)分批讀取的實現(xiàn)示例
在處理大量數(shù)據(jù)時,直接從數(shù)據(jù)庫一次性讀取所有數(shù)據(jù)可能會導(dǎo)致內(nèi)存溢出或者性能下降,本文就來介紹一下java數(shù)據(jù)庫數(shù)據(jù)分批讀取的實現(xiàn)示例,感興趣的可以了解一下2024-01-01
修改idea的這些啟動參數(shù),令你的idea健步如飛
這篇文章主要介紹了修改idea的這些啟動參數(shù),令你的idea健步如飛~具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Java設(shè)計模式之命令模式_動力節(jié)點Java學(xué)院整理
命令模式就是對命令的封裝,下文中給大家介紹了命令模式類圖中的基本結(jié)構(gòu),對java設(shè)計模式之命令模式相關(guān)知識感興趣的朋友一起看看吧2017-08-08
SpringBoot可視化接口開發(fā)工具magic-api的簡單使用教程
作為Java后端開發(fā),平時開發(fā)API接口的時候經(jīng)常需要定義Controller、Service、Dao、Mapper、XML、VO等Java對象。有沒有什么辦法可以讓我們不寫這些代碼,直接操作數(shù)據(jù)庫生成API接口呢?今天給大家推薦一款工具magic-api,來幫我們實現(xiàn)這個小目標!2021-06-06

