小编给大家分享一下怎么在拦截器中获取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); } }
请求路径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 {}
以上是“怎么在拦截器中获取url路径里面@PathVariable的参数值”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。