问题描述
我正在准备一个有两个关系表的项目.关系类型-一对多.这是一个图书馆系统.我有一张桌子用来放书,一张桌子用来给客户.
I am preparing a project where I have two relational tables. Type of relation - one to many. This is a library system. I have one table for books and one table for clients.
rent-form.jsp:
rent-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:
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.而且,在此表格中,我要添加租赁日期和返回日期.
What I want to do, I want to get the clients from the drilldown, then I would like to chose (I am choosing in drilldown menu) and add the client to the book, by using the theBook.setClient(theClient). In this case in the "book" table I should have a client id. What is more, in this form I want to add the rental date and return date.
数据库图:
我不确定我的方法是否正确,因为现在我收到了错误消息:
I am not sure if my approach is correct, for now I received the error message:
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不知道客户模型在书本形式中的作用.你们对我有什么建议吗?
I am suppose that it is related with the the model in one form. Perhaps Spring doesn't know what the client model are doing in the book form. Do you have guys any advice for me please?
推荐答案
您需要创建并注册一个转换器.
You need to create and register a converter.
您正在绑定选择
<form:select path="client">
到book中的client
属性.浏览器仅发送ID,因此您需要告诉Spring如何将该ID转换为Client实例.
to the client
property in book. The browser only sends the id so you need to tell Spring how to convert this ID to a Client instance.
请参见6.5.1 Converter SPI . html"rel =" nofollow noreferrer>"> https://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/validation.html
See 6.5.1 Converter SPI in https://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/validation.html
包org.springframework.core.convert.support;
package 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框架将转换器返回的客户端自动绑定到该书.
With that in place your controller code is simply as below i.e. the client returned by the converter is automatically bound to the book by the MVC framework.
@PostMapping("/rentBook")
public String rentBook(@ModelAttribute("book") Book theBook) {
bookService.saveBook(theBook);
return "redirect:/book/list-books";
}
这篇关于Spring MVC:如何在一个表中连接两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!