java - Spring MVC:如何在一张表中连接两个模型
问题描述
我正在准备一个有两个关系表的项目。关系类型 - 一对多。这是一个图书馆系统。我有一张放书的桌子和一张给客户的桌子。
租金-form.jsp:
<h1>${book.title}</h1>
<form:form action="rentBook" modelAttribute="book" method="POST">
<!-- need to associate this data with customer id -->
<form:hidden path="id" />
<table>
<tbody>
<tr>
<td><label>Rental Date:</label></td>
<td><spring:bind path="book"><form:input path="rentalDate" /></spring:bind></td>
</tr>
<tr>
<td><label>Return Date:</label></td>
<td><spring:bind path="book"><form:input path="returnDate" /></spring:bind></td>
</tr>
<tr>
<td><label>Client:</label></td>
<td>
<form:select path="client">
<form:option value="NONE" label="--- Select ---" />
<c:forEach var="tempClient" items="${client}">
<form:option value="${tempClient.id}">${tempClient.id} ${tempClient.firstName} ${tempClient.lastName}</form:option>
</c:forEach>
</form:select>
</td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" value="Save" class="save" /></td>
</tr>
</tbody>
</table>
</form:form>
BookController.java:
@RequestMapping("/showFormForRent")
public String showFormForRent(@RequestParam("bookId") int theId, Model theModel) {
List<Client> theClients = bookService.getClients();
theModel.addAttribute("client", theClients);
Book theBook = bookService.getBook(theId);
theModel.addAttribute("book", theBook);
return "rent-form";
}
@PostMapping("/rentBook")
public String rentBook(@ModelAttribute("book") Book theBook, @ModelAttribute("client") Client theClient) {
theBook.setClient(theClient);
bookService.saveBook(theBook);
return "redirect:/book/list-books";
}
我想要做什么,我想从钻取中获取客户端,然后我想选择(我在钻取菜单中选择)并使用 theBook.setClient(theClient) 将客户端添加到书中。在这种情况下,在“书”表中,我应该有一个客户 ID。更重要的是,在这个表格中,我想添加租赁日期和归还日期。
数据库图:
我不确定我的方法是否正确,现在我收到了错误消息:
HTTP Status 400 – Bad Request
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
我想它与一种形式的模型有关。也许 Spring 不知道客户端模型在书本形式中在做什么。请问各位大侠有什么建议吗?
解决方案
您需要创建并注册一个转换器。
您正在绑定选择
<form:select path="client">
到client
书中的属性。浏览器只发送 id,因此您需要告诉 Spring 如何将此 ID 转换为 Client 实例。
请参阅https://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/validation.html中的6.5.1 转换器 SPI
包 org.springframework.core.convert.support;
final class StringToClient implements Converter<String, Client> {
//wire in repository
public Client convert(String source) {
return clientRepo.find(Integer.valueOf(source));
}
}
有了这些,您的控制器代码就如下所示,即转换器返回的客户端由 MVC 框架自动绑定到书。
@PostMapping("/rentBook")
public String rentBook(@ModelAttribute("book") Book theBook) {
bookService.saveBook(theBook);
return "redirect:/book/list-books";
}
推荐阅读
- c++ - 为什么移动构造函数需要声明为 noexcept 以供 std::vector 使用?
- node.js - npm install 在不同机器之间的 package-lock.json 中产生了奇怪的差异
- python - VisPy中图像像素坐标系中的文本标签
- jenkins - 无法更新詹金斯“java.lang.IllegalArgumentException:文件参数'文件不可写:'/var/lib/jenkins/plugins/junit.jpi'”
- python - 如何以 Pythonian 方式确定最佳数学模型?
- apache-iotdb - 当计划无法放入 IoTDB 中的 wal 缓冲区时如何设置 wal_buffer_size
- linux - 在 ssh 连接期间是否可以从服务器端执行客户端命令?
- rust - 是否应该使依赖于特定值的函数变得不安全?
- javascript - 如何在链式地图和过滤器之间“传递”额外数据
- sockets - TURN和STUN服务器相同的端口?