在 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);
}