@ModelAttribute对象字段未绑定,并且在控制器中为空

我有一个名为“ buyRequests”的“ BuyStocks”对象列表,我正在使用th:each进行迭代,并使用名称“ Stock”进行引用。

我也有一个单独的“ SellStocks”对象名称“ soldStocks”。每个“ buyRequests”对象属性都填充在“ soldStocks”字段中,并在视图中显示有其自己的“出售”按钮。

当我单击“销售”按钮时,我希望将填充的字段保存在“ soldStocks”对象中,并通过控制器“发布”功能中的“ @ModelAttribute SellStock soldStocks”参数获取。

问题是在控制器中,“ soldStocks”对象属性为空。

@ModelAttribute对象字段未绑定,并且在控制器中为空

(找到解决方案后,这些字段将被隐藏)

控制器:

@RequestMapping(value="/{userEmail}/sell/{stockName}",method=RequestMethod.POST)
public String postSell(@PathVariable String userEmail,@PathVariable String stockName,@ModelAttribute SellStocks soldStocks,ModelMap model)

查看:

<div class="card-columns">
    <div class="card border-dark mb-3" th:each="Stock : ${buyRequests}">
        <div th:if="${!Stock.id.userEmail.equals(userEmail)}">
            <div class="card-header" th:text="${'Stock Name: ' + Stock.id.stockName}"></div>
            <div class="card-body text-dark">
                <h6 class="card-subtitle mb-2 text-muted" th:text="${'Buyer: '+ Stock.id.userEmail}"></h6>
                <h6 class="card-soubtitle mb-2 text-muted" th:text="${'Buying Price: ' + Stock.price}"></h6>
                <p class="card-text" th:text="${'Required Quantity: ' + Stock.quantity}"></p>

                <form class="text-center border border-light p-3 form-row"
                   th:object="${soldStocks}" th:action="${'/'+{userEmail}+'/sell/'+{Stock.id.stockName}}" method="post">

                    <input th:id="${soldStocks.price}" name="${soldStocks.price}" th:value="${Stock.price}" />
                    <input th:id="${soldStocks.quantity}" name="${soldStocks.quantity}" th:value="${Stock.quantity}" />
                    <input th:id="${soldStocks.id.userEmail}" name="${soldStocks.id.userEmail}" th:value="${Stock.id.userEmail}" />
                    <input th:id="${soldStocks.id.stockName}" name="${soldStocks.id.stockName}"  th:value="${Stock.id.stockName}" />

                    <button type="submit" class="btn btn-sm btn-outline-secondary my-1">Sell</button>
                </form>
            </div>
        </div>
    </div>
</div>

我正在努力的领域:

<input  th:id="${soldStocks.price}" name="${soldStocks.price}"  th:value="${Stock.price}"/>
<input  th:id="${soldStocks.quantity}" name="${soldStocks.quantity}" th:value="${Stock.quantity}"/>
<input  th:id="${soldStocks.id.userEmail}" name="${soldStocks.id.userEmail}" th:value="${Stock.id.userEmail}"/>
<input  th:id="${soldStocks.id.stockName}" name="${soldStocks.id.stockName}" th:value="${Stock.id.stockName}"/>

zqstella 回答:@ModelAttribute对象字段未绑定,并且在控制器中为空

在表单标签

中添加th:object =“ $ {soldStocks}”
本文链接:https://www.f2er.com/3151813.html

大家都在问