Fre*_*ddy 1 java spring spring-mvc spring-boot
我正在努力从 url 中获取一个 id 以用作我的 read() 方法中的参数。我阅读并看到了使用 @PathVariable 的示例的 dusin,但我不明白为什么这不起作用。
这是我的控制器类。
@GetMapping("details/{id}")
public String read(@PathVariable int employeeId, Model model)
{
model.addAttribute("students_data", studentsRepo.read(employeeId));
//the line underneath will work using as an example the int 2 in the parameter. But I want the int coming from the url.
//model.addAttribute("students_data", studentsRepo.read(2));
return "details";
}
Run Code Online (Sandbox Code Playgroud)
我在详细信息页面上收到错误消息:
Fri Jan 03 12:13:44 CET 2020
There was an unexpected error (type=Not Found, status=404).
No message available
Run Code Online (Sandbox Code Playgroud)
网址的外观示例如下:
http://localhost:8080/details?id=2
Run Code Online (Sandbox Code Playgroud)
http://localhost:8080/details?id=2您共享的 URL包含 @RequestParam 而不是 @PathVariable
如果你想使用@RequestParam 那么你的 API 签名应该是
@GetMapping("details/")
public String read(@RequestParam("id") int employeeId, Model model)
{
"details";
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用 @PathVariable 那么你的 API 应该是
@GetMapping("details/{id}")
public String read(@PathVariable("id") int employeeId, Model model)
{
"details";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2691 次 |
| 最近记录: |