I am new to Spring MVC, I have a controller displaying a single user detail or all the users as a list
// without passing any parameter the app should display all users
@RequestMapping("/user")
public String listUsers(Model m) {
List<User> users = userService.getUsers();
m.addAttribute(users);
return "userlist";
}
//same url but with userId parameter, the app displays a single user detail
@RequestMapping("/user")
public String userDetail(@RequestParam("userId") String userId, Model m)
throws IOException {
User user = userService.getUserById(userId);
m.addAttribute(user);
return "user_detail";
}
in fact I got a error "spring ambiguous mapping", my mapping syntax is definitely wrong, my question is my desired functionality can be achieved in Spring or not.
答案 0 :(得分:0)
我想说将列表请求指定为
是正确的@RequestMapping("/user")
和特定用户请求
@RequestMapping("/user/{userId}")
将userId作为路径参数传递。
答案 1 :(得分:0)
您可以更正第二种方法,例如
@RequestMapping(value={"/user/{userId}"})
public String userDetail(@PathVariable String userId, Model m)
throws IOException {
User user = userService.getUserById(userId);
m.addAttribute(user);
return "user_detail";
}