簡體   English   中英

獲取URL路徑的最后一部分

[英]Get last part of URL path

我正在嘗試使用此請求處理程序獲取用戶之后的最后一個字符串。 是否有可能做到這一點而又不被束縛?

 @Controller
 @RequestMapping("/user/*")
 public class Student{
      @RequestMapping(method = RequestMethod.GET)
      public String getUser(ModelMap model, /*, parameter that gets user id */) {
          // function that gets user id
          model.addAttribute("foo", "foo");
          return "bar";
      }
}

Spring具有@PathVariable

@Controller
 public class Student{
      @RequestMapping("/user/{id}")
      public String getUser(ModelMap model, @PathVariable String id) {
          // function that gets user id
          model.addAttribute("foo", "foo");
          return "bar";
      }
}

在這里,我假設用戶ID是String類型,您可以根據需要更改id類型。

嘗試像這樣使用@RequestParam

  @Controller
  public class Student{
  @RequestMapping(value="/user/{user_id}", method = RequestMethod.GET)
  public String getUser(ModelMap model, @RequestParam(value="user_id") Long user_id){
      // function that gets user id
      model.addAttribute("foo", "foo");
      return "bar";
  }
}

或使用@PathVariable

 @Controller
  public class Student{
  @RequestMapping(value="/user/{user_id}", method = RequestMethod.GET)
  public String getUser(ModelMap model, @PathVariable("user_id") Long user_id){
      // function that gets user id
      model.addAttribute("foo", "foo");
      return "bar";
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM