Spring MVC – Phần 3: Annotation(2) – Annotation @PathVariable, @RequestParam trong Spring MVC

Spring MVC – Phần 3: Annotation(2) – Annotation @PathVariable, @RequestParam trong Spring MVC

1. Annotation @PathVariable

Annotation PathVariable được sử dụng để xử lý những URI động, có một hoặc nhiều paramter bên trong URI.

Ví dụ:

@RequestMapping("/test1/{id}")
public String test1(@PathVariable("id") int id, Model model) {
  model.addAttribute("id", id);
  return "test1";
}

@RequestMapping("/test2/{id}/{name}")
public String test2(@PathVariable("id") int id, @PathVariable("name") String name, Model model) {
  model.addAttribute("id", id);
  model.addAttribute("name", name);
  return "test2";
}

2. Annotation @RequestParam

Khi submit method GET, trên URL sẽ chứa các giá trị của các ô input được submit. Sử dụng Annotation RequestParam giúp chúng ta lấy được giá trị đó.

(Lưu ý: với trường hợp Submit form với method = GET, bạn vẫn có thể sử dụng @ModelAttribute để ép các tham số thành đối tượng)

Ví dụ:

@RequestMapping("/test2/{id}/{name}")
public String test2(@PathVariable("id") int id, @PathVariable("name") String name, Model model) {
  model.addAttribute("id", id);
  model.addAttribute("name", name);
  return "test2";
}

3. Demo

package stackjava.com.springmvchello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HomeController {

  @RequestMapping("/")
  public String home() {
    return "home";
  }

  @RequestMapping("/test1/{id}")
  public String test1(@PathVariable("id") int id, Model model) {
    model.addAttribute("id", id);
    return "test1";
  }

  @RequestMapping("/test2/{id}/{name}")
  public String test2(@PathVariable("id") int id, @PathVariable("name") String name, Model model) {
    model.addAttribute("id", id);
    model.addAttribute("name", name);
    return "test2";
  }

  @RequestMapping("/test3")
  public String test3(@RequestParam("name") String name, @RequestParam("id") int id, Model model) {
    model.addAttribute("id", id);
    model.addAttribute("name", name);
    return "test3";
  }

}

Các file view:

<html>
<head>
<title>Spring MVC Annotation</title>
</head>

<body>
  <h2>home.jsp</h2>
  <a href="test1/1">/test1/1</a>
  <br />
  <a href="test2/2/kai">/test2/2/kai</a>
  <br />
  <fieldset>
    <legend>/test3?id=&name=</legend>
    <form action="test3">
      Id: <input type="number" name="id" /> <br /> 
      Name: <input type="text" name="name" /> <br /> 
      <input type="submit" value="submit" />
    </form>

  </fieldset>
</body>
</html>
<html>
<head>
<title>Spring MVC Annotation</title>
</head>

<body>
  <h2>test1.jsp</h2>
  Id: ${id}
  <br />
</body>
</html>
<html>
<head>
<title>Spring MVC Annotation</title>
</head>

<body>
  <h2>test2.jsp</h2>
  Id: ${id} <br>
  Name: ${name} <br/>
</body>
</html>
<html>
<head>
<title>Spring MVC Annotation</title>
</head>

<body>
  <h2>test3.jsp</h2>
  Id: ${id} <br>
  Name: ${name} <br/>
</body>
</html>

Kết quả:

Spring MVC - Phần 3: Annotation(2) - Annotation @PathVariable, @RequestParam trong Spring MVC

Download code ví dụ trên tại đây

References:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

https://www.journaldev.com/3358/spring-requestmapping-requestparam-pathvariable-example

stackjava.com