请写一下ssm框架中controller层的代码
时间: 2023-03-31 10:01:56 浏览: 158
以下是一个简单的 SSM 框架中 Controller 层的示例代码:
```
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String getUserById(@PathVariable("id") int id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "user";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user") User user) {
userService.addUser(user);
return "redirect:/user/" + user.getId();
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String updateUser(@PathVariable("id") int id, @ModelAttribute("user") User user) {
user.setId(id);
userService.updateUser(user);
return "redirect:/user/" + id;
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String deleteUser(@PathVariable("id") int id) {
userService.deleteUser(id);
return "redirect:/user";
}
}
```
注意,这只是一个示例代码,实际上 Controller 层的代码会根据具体的业务需求而有所不同。
阅读全文