简体   繁体   English

春季MVC: <spring:bind> 以及两种形式的模型属性

[英]Spring MVC: <spring:bind> and two model attributes in one form

I have prepared a form to handle two models: 我准备了一个表格来处理两个模型:

<form action="rentBook2" method="post">
        <table>
            <tbody>
                    <tr>
                    <spring:bind path="book.id">
                       <input value="${book.id}" type="hidden" name="id"/><br />
                    </spring:bind>

                    <spring:bind path="client.id">
                       <input value="${client.id}" type="hidden" name="id"/><br />
                    </spring:bind>

                    <td><label>Rental Date:</label></td>
                    <td>            
                    <spring:bind path="book.rentalDate">
                       <input type="text" name="rentalDate"/><br />
                    </spring:bind>

                    <tr>
                    <td><label>Return Date:</label></td>
                    <td>
                    <spring:bind path="book.returnDate">
                       <input type="text" name="returnDate"/><br />
                    </spring:bind>
                    </td>

                    <tr><td></td>
                        <td><input type="submit" value="Submit"/></td>
                    </tr>
            </tbody>
        </table>
        </form>

For better understanding: book.id and client.id are hidden because this value is already passing by get method and request param in url from previus form. 为了更好地理解:book.id和client.id被隐藏,因为此值已经通过get方法传递,并且从上一表格中以url请求参数。

But the problem is: I am able to receive book.id, book.rentalDate and book.returnDate from book input. 但是问题是:我可以从输入的书中接收book.id,book.rentalDate和book.returnDate。 What is wired, client.id has the same value as book.id (what is of course incorrect). 有线的client.id与book.id具有相同的值(这当然是不正确的)。

This is my controller method: 这是我的控制器方法:

@PostMapping("/rentBook2")
public String rentBook2(@ModelAttribute("book") Book theBook, @ModelAttribute("client") Client theClient) {

    System.out.println("This is book in controller : " + theBook);
    System.out.println("This is client in controller : " + theClient);

    bookService.rentBook(theBook.getId(), theClient.getId());
    bookService.saveBook(theBook);

    return "redirect:/book/list-books";
}

And the example output from println: 而println的示例输出:

This is book in controller : Book [id=19, title=null, author=null, genre=null, rentalDate=20-02-2019, returnDate=25-02-2019, client=null]
This is client in controller : Client [id=19, firstName=null, lastName=null, email=null]

The value "19" which you can see above is from ${book.id} but the correct one for ${client.id} should be "6". 您在上面看到的值“ 19”来自$ {book.id},但$ {client.id}的正确值应为“ 6”。

Have you any idea why the book.id and client.id have the same value? 您知道为什么book.id和client.id具有相同的值吗?

As advised by @M. 如@M所建议。 Deinum, the problem was in not enough unique field names. Deinum,问题出在唯一字段名称不足。 I have changed from id to idBook and from id to idClient and the mechanism is working as expected. 我从id更改为idBook,从id更改为idClient,该机制按预期工作。

Thanks @M. 谢谢@M。 Deinum! Deinum!

我的建议是将瞬态变量添加到您的任何类中,以便您可以通过一个模型属性(所需类的一个对象)处理每个值,然后对该对象执行所有逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM