@ModelAttribute
当用户想要在数据库(书表)中添加新书时,我正在尝试使用注释绑定用户输入。Book 类有四个属性(int bookId, String title, Author author, Publisher publisher
)。用户只需输入标题,然后从下拉列表中选择作者和出版商。提交表单时,控制器中的模型对象只有标题值,作者和发布者为空。我在下面展示了我的表单和请求处理程序方法。谁能说出我的表单或控制器类中的问题在哪里?
addBook.jsp
<form action="addBookExecution" method="post">
<table>
<tr>
<td>Enter Book's Name:</td>
<td><input type="text" name="title"/></td>
</tr>
<tr>
<td>Select Author: </td>
<td><select name="author">
<option>****select author****</option>
<c:forEach var="author" items="${authorList}">
<option value="${author}">${author.authorName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>Select Publisher: </td>
<td><select name="publisher">
<option>****select publisher****</option>
<c:forEach var="publisher" items="${publisherList}">
<option value="${publisher}">${publisher.publisherName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>Click here to submit the form:</td>
<td><input type="submit" value="submit"/></td>
</tr>
控制器:
@RequestMapping(value = "/addBookExecution", method = RequestMethod.POST)
protected ModelAndView addBookExecution(@ModelAttribute Book book)
throws ClassNotFoundException, SQLException {
bookService.addBook(book);
ModelAndView model = new ModelAndView("adminFunctionsPage");
model.addObject("Msg","Your request has been processed successfully.");
return model;
}