У меня есть один контроллер под названием IndexController. У него есть методы @GetMapping и @PostMapping. Когда пользователь вводит URL для перехода к / index, он загружает список книг. На этой странице указателя есть кнопка «Добавить новую книгу», которая вызывает модальное всплывающее окно. Когда пользователь вводит информацию в форму и нажимает кнопку «Добавить», он должен вызвать метод @PostMapping. Это не вызов метода. Модель для формы - IndexBook.
Класс IndexController:
@Controller
public class IndexController {
@GetMapping("/index")
public String formGet(Model model) {
BookService bookService = new BookService();
ArrayList<ReviewedBook> reviewedBooks = bookService.getRecordedBooks(1);
model.addAttribute("indexbook", new IndexBook());
model.addAttribute("books", reviewedBooks);
return "index";
}
@PostMapping("/index")
public String formPost(@ModelAttribute IndexBook ib, Model model) {
System.out.println("Post for index called.");
BookService bookService = new BookService();
return "index";
}
}
IndexBook:
public class IndexBook {
private int userId;
private String title;
private String author;
private String isbn;
private String genre;
private String dateRead;
private int rating;
public void setUserId(int userId) {
this.userId = userId;
}
public int getUserId() {
return userId;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAuthor() {
return author;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getIsbn() {
return isbn;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getGenre() {
return genre;
}
public void setDateRead(String dateRead) {
this.dateRead = dateRead;
}
public String getDateRead() {
return dateRead;
}
public void setRating(int rating) {
this.rating = rating;
}
public int getRating() {
return rating;
}
}
index.html (прокрутите немного вниз до комментария 'Модальное содержимое')
<!DOCTYPE html>
<html lang="en">
<head>
<title>Book Note Book</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/index_styles.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index">Book Note Book</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="index">Books</a></li>
<li><a href="badges">Badges</a></li>
<li><a href="wishlist">Wishlist</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="profile">Profile</a></li>
<li><a href="#"><span class="glyphicon glyphicon-off"></span> Logout</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h2>Books</h2>
<p>This is all the books you've read</p>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Genre</th>
<th>ISBN</th>
<th>Date read</th>
<th>Rating</th>
</tr>
</thead>
<tbody>
<tr th:if="${books.empty}">
<td colspan="2"> No Books Available </td>
</tr>
<tr th:each="book : ${books}">
<td><p th:text="${book.title}"/></td></td>
<td><p th:text="${book.author}"/></td>
<td><p th:text="${book.genre}"/></td>
<td><p th:text="${book.isbn}"/></td>
<td><p th:text="${book.dateRead}"/></td>
<td><p th:text="${book.rating}"/></td>
</tr>
</tbody>
</table>
</div>
<div class="container">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addNewBook">Add new book</button>
<div class="modal fade" id="addNewBook" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add a new book</h4>
</div>
<div class="modal-body">
**<form action="#" class="form-horizontal" th:action="@{/index}" th:object="${indexbook}" method="post">
<input type="hidden" th:field="*{userId}" />
<div class="form-group">
<label class="control-label col-sm-2" for="title">Title</label>
<div class="col-sm-10">
<input type="text" th:field="*{title}" class="form-control" id="title" placeholder="Enter title" name="title">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="author">Author</label>
<div class="col-sm-10">
<input type="text" th:field="*{author}" class="form-control" id="author" placeholder="Enter author" name="author">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="genre">Genre</label>
<div class="col-sm-10">
<input type="text" th:field="*{genre}" class="form-control" id="genre" placeholder="Enter genre" name="genre">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="isbn">ISBN</label>
<div class="col-sm-10">
<input type="text" th:field="*{isbn}" class="form-control" id="isbn" placeholder="Enter ISBN" name="isbn">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="dateRead">Date read</label>
<div class="col-sm-10">
<input type="text" th:field="*{dateRead}" class="form-control" id="dateRead" placeholder="Enter date read" name="Enter the date read">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="rating">Rating</label>
<div class="col-sm-10">
<input type="text" th:field="*{rating}" class="form-control" id="rating" placeholder="Enter rating" name="Enter the rating">
</div>
</div>
</form>**
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Add</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="footer navbar-fixed-bottom">
<footer class="container-fluid text-center">
<p>© 2018 Book Note Book</p>
</footer>
</div>
</body>
</html>