【问题标题】:How do i get the requestmapping value in the controller?如何在控制器中获取 requestmapping 值?
【发布时间】:2011-04-17 07:53:07
【问题描述】:

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

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

【问题讨论】:

  • 能否请您扩展您的用例?我试图弄清楚你想要在这里得到什么,因为除了记录或使用完整路径之外,搜索似乎没有用,在这种情况下,你可以按照 ankit 的指示从请求中获取路径

标签: spring-mvc


【解决方案1】:

看来你是在找这个请求匹配的路径,那你可以直接从servlet路径中获取

@RequestMapping("/search/")     
 public Map searchWithSearchTerm(@RequestParam("name") String name, HttpServletRequest request) {    
String path = request.getServletPath();
        // more code here     
 }

【讨论】:

  • 可以说 req 映射类似于 @RequestMapping("/search/{userId}") 在这种情况下,我们将使用 .获取Servlet路径。关于我们如何获得 /search/{id} 的任何想法?
  • 你为什么要这样做..它只是一个变量名..
【解决方案2】:

对于 Spring 3.1 及更高版本,您可以使用 ServletUriComponentsBuilder

@RequestMapping("/search/")     
     public ResponseEntity<?> searchWithSearchTerm(@RequestParam("name") String name) {  
         UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest();
         System.out.println(builder.buildAndExpand().getPath());
         return new ResponseEntity<String>("OK", HttpStatus.OK);
     }

【讨论】:

    【解决方案3】:

    拥有像这样的控制器

    @Controller
    @RequestMapping(value = "/web/objet")
    public class TestController {
    
        @RequestMapping(value = "/save")
        public String save(...) {
            ....
        }
    }
    

    您无法使用反射获取控制器基础 requestMapping

    // Controller requestMapping
    String controllerMapping = this.getClass().getAnnotation(RequestMapping.class).value()[0];
    

    或带有反射的方法 requestMapping(从方法内部)

    //Method requestMapping
    String methodMapping = new Object(){}.getClass().getEnclosingMethod().getAnnotation(RequestMapping.class).value()[0];
    

    显然适用于 requestMapping 中的单个值。

    希望这会有所帮助。

    【讨论】:

    • 这在使用具有内部逻辑的抽象基本控制器时非常有用,需要继承的具体控制器类的请求映射
    • 类方法 public &lt;A extends Annotation&gt; A getAnnotation(Class&lt;A&gt; annotationClass) 注释必须扩展 Annotation 而 RequestMapping 没有。真的可以class.getAnnotation(RequestMapping.class)吗?
    • @Tiina 我必须将getAnnotation() 的结果转换为RequestMapping 对象,以便使用.value() 方法并获取第一个条目。示例:String requestMapping = ((RequestMapping) testControllerClass.getAnnotation(RequestMapping.class)).value()[0];
    【解决方案4】:

    如果你想要图案,可以试试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);
    
    }
    

    【讨论】:

      【解决方案5】:
      @RequestMapping("foo/bar/blub")     
      public Map searchWithSearchTerm(@RequestParam("name") String name, HttpServletRequest request) {
        // delivers the path without context root 
        // mapping = "/foo/bar/blub"
        String mapping = request.getPathInfo();
        // more code here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-12
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2012-02-17
        相关资源
        最近更新 更多