服务器之家

服务器之家 > 正文

如何在拦截器中获取url路径里面@PathVariable的参数值

时间:2021-11-29 13:10     来源/作者:nimo10050

在拦截器中获取url路径里@PathVariable参数值

解决办法

?
1
2
3
Map pathVariables = (Map) request.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String classId = (String)pathVariables.get("classId");

示例接口

?
1
2
3
4
5
// 获取某个班级下面的学生列表
@RequestMapping("/classes/{classId}/students")
public String list(@PathVariable String classId){
    return "学生列表";
}

完整示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
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为变量值:

?
1
@RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
?
1
2
Tool tool = new Tool();
tool.setUrls(new String[]{"finance/account/loanDownExcel/cur","finance/account/loanDownExcel/all"});
?
1
2
@RequestMapping("accountlog/loanDownExcel/{type}")
public void exportAccountLogExcel(@PathVariable("type")  String type)throws Exception {}

或者是

?
1
public void exportAccountLogExcel(@PathVariable   String type)throws Exception {}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/cnm10050/article/details/106344305

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部