欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-08-30 09:29:33|栏目:JAVA代码|点击:

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

解决办法

Map pathVariables = (Map) request.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String classId = (String)pathVariables.get("classId");

示例接口

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

完整示例

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 {}

上一篇:Java 超详细讲解十大排序算法面试无忧

栏    目:JAVA代码

下一篇:Java基本数据类型与类型转换实例分析

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

本文地址:http://www.codeinn.net/misctech/212285.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有