【问题标题】:Get requested value(URL) when using @RequestMapping annotations使用@RequestMapping 注解时获取请求的值(URL)
【发布时间】:2011-11-27 10:06:06
【问题描述】:

当我将多个值映射到@RequestMapping(如Multiple Spring @RequestMapping annotations)时,我可以获得请求的值(URL)吗?

像这样:

@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET)
public String getCenter(Model model) throws Exception {     
    String requestedValue = getRequestedValue();  // I want this.

    // I want to do something like this with requested value.
    String result; 
    if (requestedValue.equals("center")
        result = "center";
    else if (requestedValue.equals("left")
        result = "left";
    return result;
}

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    您可以将请求 (HttpServletRequest) 本身作为处理程序方法的参数。因此,您可以检查请求 url 以获取“值”。

    @RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET)
    public String getCenter(Model model, HttpServletRequest request) throws Exception {             
       String whatYouCallValue = request.getServletPath(); 
       ....
    

    Javadoc:https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getServletPath--

    顺便说一句:如果我理解你的话,你想要不同的 url,而不是不同的值。

    【讨论】:

    • 谢谢它的工作。我称它为值是因为属性的名称是值,但现在我认为它可能会造成混淆。我会纠正一些。
    【解决方案2】:

    从 Spring 3.1.0 开始,您可以使用ServletUriComponentsBuilder

    @RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET)
        public String getCenter(Model model) throws Exception {     
            UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest();
            String requestedValue = builder.buildAndExpand().getPath();  // I want this.
            System.out.println(requestedValue);
            // I want to do something like this with requested value.
            String result="fail"; 
            if (requestedValue.equals("center"))
                result = "center";
            else if (requestedValue.equals("left"))
                result = "left";
            return result;
        }
    

    【讨论】:

    • 这正是我想要的。我不想用内部的东西弄乱 API 映射定义。
    • ServletUriComponentsBuilder.fromRequest(HttpServletRequest request) 在拦截器中运行良好。
    【解决方案3】:

    遵循正则表达式将使您的方法仅对 URL /center/left 执行。并且可以通过@PathVariable注解获取值。

    @GetMapping("/{path:^center$|^left$}")
    public ResponseEntity<?> whatIsThePath(@PathVariable String path){
        // path is either "center" or "left"
    }
    

    【讨论】:

      【解决方案4】:

      除了最佳答案@Hugh_Lee: 此方法适用于所有未映射的请求。如果您只想将此方法用于两种(或几种)情况,例如“/center”和“/left”,你可以做以下。将“center”重命名为“positionCenter”,将“left”重命名为“positionLeft”(或添加另一个常用词)。所以代码是这样的:

      @RequestMapping(value={"/{path:position+[A-Za-z-]+}"}, method=RequestMethod.GET)
      public String getCenter(@PathVariable String path) throws Exception {             
          // "path" is what I want
      }
      

      【讨论】:

        【解决方案5】:

        从 Spring 3.1.0 开始,您可以使用URI Template Patterns with Regular Expressions

        @RequestMapping(value={"/{path:[a-z-]+}"}, method=RequestMethod.GET)
        public String getCenter(@PathVariable String path) throws Exception {             
            // "path" is what I want
        }
        

        【讨论】:

          【解决方案6】:

          使用 RequestParam 注释。您还可以将 HttpServletRequest 类型的参数添加到您的方法中,然后从中获取参数。

          【讨论】:

          • 似乎 Sangdol 表示不同的 url,而不是 http 值。 (所以他的“价值观”这个词有点混乱)
          猜你喜欢
          • 1970-01-01
          • 2018-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多