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

网友投稿 417 2022-10-03


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

目录在拦截器中获取url路径里@PathVariable参数值解决办法示例接口完整示例spring @PathVariable:请求路径url 上有变量值,通过@PathVariable获取示例:

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

解决办法

Map pathVariables = (Map)http:// 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.shttp://ervlet.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 sIeJQyGtub

}

}

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 thttp://ool = 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 {}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于信息安全,这些你都了解吗?(对信息安全的了解)
下一篇:做足了边界安全防护,电力行业应对内网威胁感知还需什么准备?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~