Skip to main content
 首页 » 编程设计

spring-mvc之如何在 Controller 中获取请求映射值

2024年11月01日33davidwang456

在 Controller 中,我有这个代码,
不知何故,我想获得请求映射值“搜索”。
这怎么可能 ?

 @RequestMapping("/search/")      
 public Map searchWithSearchTerm(@RequestParam("name") String name) {     
        // more code here      
 } 

请您参考如下方法:

如果你想要图案,你可以试试HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE :

@RequestMapping({"/search/{subpath}/other", "/find/other/{subpath}"}) 
public Map searchWithSearchTerm(@PathVariable("subpath") String subpath, 
                                             @RequestParam("name") String name) { 
 
    String pattern = (String) request.getAttribute( 
                                 HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); 
    // pattern will be either "/search/{subpath}/other" or 
    // "/find/other/{subpath}", depending on the url requested 
    System.out.println("Pattern matched: "+pattern); 
 
}