SSM框架书架管理的Controller层代码实现
时间: 2023-09-20 11:10:04 浏览: 143
j211002_ssmdemo01.zip
好的,以下是一个简单的SSM框架的图书管理系统的Controller层代码实现示例:
```java
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/list")
public String list(Model model) {
List<Book> bookList = bookService.getAllBooks();
model.addAttribute("bookList", bookList);
return "book/list";
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("book", new Book());
return "book/edit";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(Book book) {
bookService.addBook(book);
return "redirect:/book/list";
}
@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String edit(@PathVariable("id") Long id, Model model) {
Book book = bookService.getBookById(id);
model.addAttribute("book", book);
return "book/edit";
}
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(Book book) {
bookService.updateBook(book);
return "redirect:/book/list";
}
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
public String delete(@PathVariable("id") Long id) {
bookService.deleteBookById(id);
return "redirect:/book/list";
}
}
```
这段代码实现了对图书的增加、修改、删除和查询功能,其中:
- `@Controller`注解表示这是一个控制器类;
- `@RequestMapping`注解用于映射请求路径;
- `@Autowired`注解用于自动注入依赖的`BookService`服务;
- `Model`对象用于向视图中传递数据;
- `@PathVariable`注解用于获取请求路径中的参数;
- `@RequestParam`注解用于获取请求参数;
- `redirect:`关键字用于重定向到另一个请求路径。
希望这个示例能够帮助你理解SSM框架中Controller层的代码实现。
阅读全文