溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在攔截器中獲取url路徑里面@PathVariable的參數(shù)值

發(fā)布時間:2021-08-23 10:09:39 來源:億速云 閱讀:163 作者:小新 欄目:開發(fā)技術

小編給大家分享一下怎么在攔截器中獲取url路徑里面@PathVariable的參數(shù)值,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

在攔截器中獲取url路徑里@PathVariable參數(shù)值

解決辦法

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

以上是“怎么在攔截器中獲取url路徑里面@PathVariable的參數(shù)值”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI