+ * Created by bysocket + */ +@Controller +@RequestMapping(value = "/book") +public class BookController { + + private static final String BOOK_FORM_PATH_NAME = "bookForm"; + private static final String BOOK_LIST_PATH_NAME = "bookList"; + private static final String REDIRECT_TO_BOOK_URL = "redirect:/book"; + + @Autowired + BookService bookService; + + /** + * 获取 Book 列表 + * 处理 "/book" 的 GET 请求,用来获取 Book 列表 + */ + @RequestMapping(method = RequestMethod.GET) + public String getBookList(final Model model) { + model.addAttribute("bookList", bookService.findAll()); + return BOOK_LIST_PATH_NAME; + } + + /** + * 获取创建 Book 表单 + */ + @RequestMapping(value = "/create", method = RequestMethod.GET) + public String createBookForm(final Model model) { + model.addAttribute("book", new Book()); + model.addAttribute("action", "create"); + return BOOK_FORM_PATH_NAME; + } + + /** + * 创建 Book + * 处理 "/book/create" 的 POST 请求,用来新建 Book 信息 + * 通过 @ModelAttribute 绑定表单实体参数,也通过 @RequestParam 传递参数 + */ + @RequestMapping(value = "/create", method = RequestMethod.POST) + public String postBook(@ModelAttribute Book book) { + bookService.insertByBook(book); + return REDIRECT_TO_BOOK_URL; + } + + /** + * 获取更新 Book 表单 + * 处理 "/book/update/{id}" 的 GET 请求,通过 URL 中的 id 值获取 Book 信息 + * URL 中的 id ,通过 @PathVariable 绑定参数 + */ + @RequestMapping(value = "/update/{id}", method = RequestMethod.GET) + public String getUser(@PathVariable Long id, final Model model) { + model.addAttribute("book", bookService.findById(id)); + model.addAttribute("action", "update"); + return BOOK_FORM_PATH_NAME; + } + + /** + * 更新 Book + * 处理 "/update" 的 PUT 请求,用来更新 Book 信息 + */ + @RequestMapping(value = "/update", method = RequestMethod.POST) + public String putBook(@ModelAttribute Book book) { + bookService.update(book); + return REDIRECT_TO_BOOK_URL; + } + + /** + * 删除 Book + * 处理 "/book/{id}" 的 GET 请求,用来删除 Book 信息 + */ + @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) + public String deleteBook(@PathVariable Long id) { + bookService.delete(id); + return REDIRECT_TO_BOOK_URL; + } + +} diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/application.properties b/springboot-webflux-10-book-manage-sys/src/main/resources/application.properties new file mode 100644 index 0000000..270284a --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.data.mongodb.host=localhost +spring.data.mongodb.database=admin +spring.data.mongodb.port=27017 +spring.data.mongodb.username=admin +spring.data.mongodb.password=admin diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/static/css/default.css b/springboot-webflux-10-book-manage-sys/src/main/resources/static/css/default.css new file mode 100755 index 0000000..4da3051 --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/resources/static/css/default.css @@ -0,0 +1,2 @@ +/* contentDiv */ +.contentDiv {padding:20px 60px;} \ No newline at end of file diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/static/images/favicon.ico b/springboot-webflux-10-book-manage-sys/src/main/resources/static/images/favicon.ico new file mode 100755 index 0000000..e5a2934 Binary files /dev/null and b/springboot-webflux-10-book-manage-sys/src/main/resources/static/images/favicon.ico differ diff --git a/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookForm.html b/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookForm.html new file mode 100644 index 0000000..9a893eb --- /dev/null +++ b/springboot-webflux-10-book-manage-sys/src/main/resources/templates/bookForm.html @@ -0,0 +1,56 @@ + + +
+ + + + + +