Cả Request Parameter và Path Variable đều sử dụng để gửi giá trị tới server. Cả hai đều là một phần của URL. Request Parameter có dữ liệu theo cặp (key = value) được phân tách bằng dấu &.
Ví dụ: URL sau mang một request parameter là name với giá trị là codelean
  • key = name
  • value = codelean.vn
Để nhận giá trị của một request parameter sử dụng annotation @RequestParam như sau:
public String greetingwithparam(@RequestParam String name){…}
Ngoài ra còn có thể sử dụng annotation @PathVariable
public String greetingwithpathvariable(@PathVariable String name){…}
  • value = codelean.vn
Với @PathVarible không cần key truyền trên URL chỉ cần value.
Để sử dụng @PathVariable cần thêo biến thuộc tính value của annotation @RequestMapping
@RequestMapping(value = "/greetingwithpathvariable/{name}", method = GET)
public String greeting(@PathVariable String name, Model model){
    model.addAttribute("name", name);
    return "index";
}

Post a Comment

Mới hơn Cũ hơn