您好,登錄后才能下訂單哦!
小編給大家分享一下怎么在攔截器中獲取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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。