springboot图书管理系统,增加操作页面跳转代码
时间: 2023-10-11 08:18:42 浏览: 43
好的,您需要在 Spring Boot 的图书管理系统中添加一个新增图书的功能,对应的操作页面跳转代码可以参考下面的示例代码:
1. 在 Controller 类中添加新增图书的请求处理方法,如下所示:
```java
@GetMapping("/books/add")
public String showAddBookForm(Model model) {
model.addAttribute("book", new Book());
return "add-book";
}
@PostMapping("/books")
public String addBook(@ModelAttribute("book") Book book, BindingResult result) {
if (result.hasErrors()) {
return "add-book";
}
bookRepository.save(book);
return "redirect:/books";
}
```
2. 在 templates 目录下创建 add-book.html 文件,该文件用于显示新增图书的表单页面。示例代码如下:
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Add Book</title>
</head>
<body>
<h1>Add Book</h1>
<form th:action="@{/books}" th:object="${book}" method="post">
<label for="title">Title:</label>
<input type="text" th:field="*{title}" required/>
<label for="author">Author:</label>
<<input type="text" th:field="*{author}" required/>
<label for="publisher">Publisher:</label>
<<input type="text" th:field="*{publisher}" required/>
<label for="isbn">ISBN:</label>
<<input type="text" th:field="*{isbn}" required/>
<button type="submit">Add</button>
</form>
</body>
</html>
```
3. 在 books.html 页面中添加一个链接,用于跳转到新增图书的页面。示例代码如下:
```html
<a th:href="@{/books/add}">Add Book</a>
```
这样,当用户点击该链接时,就会跳转到新增图书的表单页面,用户填写完成后,点击提交按钮,就会调用 addBook() 方法将新的图书信息保存到数据库中。保存完成后,用户会被重定向到图书列表页面。