消息必需字符串参数 'searchBook' 不存在
Message Required String parameter 'searchBook' is not present
我正在尝试制作 create/remove/update 本书的 CRUD+spring 应用程序。一切正常,但从数据库中搜索一本书。请帮帮我。我的方法有什么问题 "searchBook"?
@Controller
public class BookController {
private BookService bookService;
private final int pageSize = 10;
@Autowired(required = true)
@Qualifier(value = "bookService")
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
@RequestMapping(value = {"/getAllBooks"})
public ModelAndView getAllBooks() {
List<Book> bookList = bookService.getAllBooks();
return new ModelAndView("books", "books", bookList);
}
@RequestMapping(value = "/books/add", method = RequestMethod.POST)
public String addBook(@ModelAttribute("book") Book book) {
if (book.getId() == 0) {
this.bookService.addBook(book);
} else {
this.bookService.updateBook(book);
}
return "redirect:/books";
}
@RequestMapping(value = "/remove/{id}")
public String removeBook(@PathVariable("id") int id) {
this.bookService.removeBook(id);
return "redirect:/books";
}
@RequestMapping(value = "/edit/{id}")
public String editBook(@PathVariable("id") int id, Model model) {
model.addAttribute("book", this.bookService.getBookById(id));
model.addAttribute("listBooks", this.bookService.getAllBooks());
return "books";
}
@RequestMapping(value = "bookdata/{id}")
public String bookData(@PathVariable("id") int id, Model model) {
model.addAttribute("book", this.bookService.getBookById(id));
return "bookdata";
}
@RequestMapping("searchBook")
public ModelAndView searchBook(@RequestParam("searchBook") String title){
List<Book> booksList = bookService.getAllBooks(title);
return new ModelAndView("booksearch", "booksList", booksList);
}
}
有个books.jsp:
</head>
<br/>
<h4><a href="../../index.jsp">Back to main menu</a></h4>
<br/>
<div>
<form action="searchBook">
<div class="row">
<div><h2>Search books by title:</h2></div>
<div><input type="text" title="searchBook" id="searchBook"
placeholder="type title here.."></div>
<br/>
<div><input type='submit' value='Search'/></div>
</div>
</form>
<br/>
<table class="table table-hover table-bordered">
<thead style="background-color: #b39b89;">
<tr>
<th width="80">ID</th>
<th width="120">Title</th>
<th width="120">Author</th>
<th width="120">Description</th>
<th width="60">ISBN</th>
<th width="10">Print Year</th>
<th width="120">Read Already</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
</thead>
<tbody>
<c:forEach items="${bookList}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.bookTitle}</td>
<td>${book.bookAuthor}</td>
<td>${book.description}</td>
<td>${book.isbn}</td>
<td>${book.printYear}</td>
<td>${book.readAlready}</td>
<td><a href="<c:url value='/edit/${book.id}'/>">Edit</a></td>
<td><a href="<c:url value='/remove/${book.id}'/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
我的项目结构如下:http://clip2net.com/s/3ON3F6n
BookDaoimpl 有一个 getAllBooks() 方法:
@SuppressWarnings("unchecked")
@Override
public List<Book> getAllBooks(String bookName) {
Session session = this.sessionFactory.getCurrentSession();
String query = "SELECT * FROM books WHERE BOOK_TITLE like '%" +
bookName + "%'";
List<Object[]> bookObjects = session.createQuery(query).list();
List<Book> books = new ArrayList<Book>();
for (Object[] bookObject : bookObjects) {
Book book = new Book();
int id = (int) bookObject[0];
String title = (String) bookObject[1];
String description = (String) bookObject[2];
String author = (String) bookObject[3];
int isbn = (int) bookObject[4];
int printYear = (int) bookObject[5];
int readAlready = (int) bookObject[6];
book.setId(id);
book.setBookTitle(title);
book.setDescription(description);
book.setBookAuthor(author);
book.setIsbn(isbn);
book.setPrintYear(printYear);
book.setReadAlready(readAlready);
books.add(book);
}
System.out.println(books);
return books;
}
有一个 MySQL 数据库:http://clip2net.com/s/3ON4e7o
您没有在标签中添加 name 属性。在
里面添加name="searchBook"
<form action="searchBook">
<div class="row">
<div><h2>Search books by title:</h2></div>
<div><input type="text" name="searchBook" id="searchBook"
placeholder="type title here.."></div>
<br/>
<div><input type='submit' value='Search'/></div>
</div>
</form>
当我嵌套使用 form
和 c:forEach
时,出现异常并删除了表单标签。
原来是这样的:
<frm:form modelAttribute="" action="">
<table>
<tr>
<td>Kitap ID</td>
<td>Kitabın Adı</td>
</tr>
<c:forEach items="${kitaplar}" var="kitap">
<c:url var="updateLink" value="kitap-ver">
<c:param name="kitap.ad" value="${kitap.id}" />
</c:url>
<tr>
<td>${kitap.id}</td>
<td>${kitap.ad}</td>
<td><button onclick="window.location.href='${updateLink}'"
class="update-button">Kitabı Ver</button></td>
</tr>
</c:forEach>
</table>
</frm:form>
解决方法是:
<table>
<tr>
<td>Kitap ID</td>
<td>Kitabın Adı</td>
</tr>
<c:forEach items="${kitaplar}" var="kitap">
<c:url var="updateLink" value="kitap-ver">
<c:param name="kitap.ad" value="${kitap.id}" />
</c:url>
<tr>
<td>${kitap.id}</td>
<td>${kitap.ad}</td>
<td><button onclick="window.location.href='${updateLink}'"
class="update-button">Kitabı Ver</button></td>
</tr>
</c:forEach>
</table>
我正在尝试制作 create/remove/update 本书的 CRUD+spring 应用程序。一切正常,但从数据库中搜索一本书。请帮帮我。我的方法有什么问题 "searchBook"?
@Controller
public class BookController {
private BookService bookService;
private final int pageSize = 10;
@Autowired(required = true)
@Qualifier(value = "bookService")
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
@RequestMapping(value = {"/getAllBooks"})
public ModelAndView getAllBooks() {
List<Book> bookList = bookService.getAllBooks();
return new ModelAndView("books", "books", bookList);
}
@RequestMapping(value = "/books/add", method = RequestMethod.POST)
public String addBook(@ModelAttribute("book") Book book) {
if (book.getId() == 0) {
this.bookService.addBook(book);
} else {
this.bookService.updateBook(book);
}
return "redirect:/books";
}
@RequestMapping(value = "/remove/{id}")
public String removeBook(@PathVariable("id") int id) {
this.bookService.removeBook(id);
return "redirect:/books";
}
@RequestMapping(value = "/edit/{id}")
public String editBook(@PathVariable("id") int id, Model model) {
model.addAttribute("book", this.bookService.getBookById(id));
model.addAttribute("listBooks", this.bookService.getAllBooks());
return "books";
}
@RequestMapping(value = "bookdata/{id}")
public String bookData(@PathVariable("id") int id, Model model) {
model.addAttribute("book", this.bookService.getBookById(id));
return "bookdata";
}
@RequestMapping("searchBook")
public ModelAndView searchBook(@RequestParam("searchBook") String title){
List<Book> booksList = bookService.getAllBooks(title);
return new ModelAndView("booksearch", "booksList", booksList);
}
}
有个books.jsp:
</head>
<br/>
<h4><a href="../../index.jsp">Back to main menu</a></h4>
<br/>
<div>
<form action="searchBook">
<div class="row">
<div><h2>Search books by title:</h2></div>
<div><input type="text" title="searchBook" id="searchBook"
placeholder="type title here.."></div>
<br/>
<div><input type='submit' value='Search'/></div>
</div>
</form>
<br/>
<table class="table table-hover table-bordered">
<thead style="background-color: #b39b89;">
<tr>
<th width="80">ID</th>
<th width="120">Title</th>
<th width="120">Author</th>
<th width="120">Description</th>
<th width="60">ISBN</th>
<th width="10">Print Year</th>
<th width="120">Read Already</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
</thead>
<tbody>
<c:forEach items="${bookList}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.bookTitle}</td>
<td>${book.bookAuthor}</td>
<td>${book.description}</td>
<td>${book.isbn}</td>
<td>${book.printYear}</td>
<td>${book.readAlready}</td>
<td><a href="<c:url value='/edit/${book.id}'/>">Edit</a></td>
<td><a href="<c:url value='/remove/${book.id}'/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
我的项目结构如下:http://clip2net.com/s/3ON3F6n
BookDaoimpl 有一个 getAllBooks() 方法:
@SuppressWarnings("unchecked")
@Override
public List<Book> getAllBooks(String bookName) {
Session session = this.sessionFactory.getCurrentSession();
String query = "SELECT * FROM books WHERE BOOK_TITLE like '%" +
bookName + "%'";
List<Object[]> bookObjects = session.createQuery(query).list();
List<Book> books = new ArrayList<Book>();
for (Object[] bookObject : bookObjects) {
Book book = new Book();
int id = (int) bookObject[0];
String title = (String) bookObject[1];
String description = (String) bookObject[2];
String author = (String) bookObject[3];
int isbn = (int) bookObject[4];
int printYear = (int) bookObject[5];
int readAlready = (int) bookObject[6];
book.setId(id);
book.setBookTitle(title);
book.setDescription(description);
book.setBookAuthor(author);
book.setIsbn(isbn);
book.setPrintYear(printYear);
book.setReadAlready(readAlready);
books.add(book);
}
System.out.println(books);
return books;
}
有一个 MySQL 数据库:http://clip2net.com/s/3ON4e7o
您没有在标签中添加 name 属性。在
里面添加name="searchBook"<form action="searchBook">
<div class="row">
<div><h2>Search books by title:</h2></div>
<div><input type="text" name="searchBook" id="searchBook"
placeholder="type title here.."></div>
<br/>
<div><input type='submit' value='Search'/></div>
</div>
</form>
当我嵌套使用 form
和 c:forEach
时,出现异常并删除了表单标签。
原来是这样的:
<frm:form modelAttribute="" action="">
<table>
<tr>
<td>Kitap ID</td>
<td>Kitabın Adı</td>
</tr>
<c:forEach items="${kitaplar}" var="kitap">
<c:url var="updateLink" value="kitap-ver">
<c:param name="kitap.ad" value="${kitap.id}" />
</c:url>
<tr>
<td>${kitap.id}</td>
<td>${kitap.ad}</td>
<td><button onclick="window.location.href='${updateLink}'"
class="update-button">Kitabı Ver</button></td>
</tr>
</c:forEach>
</table>
</frm:form>
解决方法是:
<table>
<tr>
<td>Kitap ID</td>
<td>Kitabın Adı</td>
</tr>
<c:forEach items="${kitaplar}" var="kitap">
<c:url var="updateLink" value="kitap-ver">
<c:param name="kitap.ad" value="${kitap.id}" />
</c:url>
<tr>
<td>${kitap.id}</td>
<td>${kitap.ad}</td>
<td><button onclick="window.location.href='${updateLink}'"
class="update-button">Kitabı Ver</button></td>
</tr>
</c:forEach>
</table>