繁体   English   中英

将每个列表发送到控制器(thymeleaf Spring Boot)

[英]Send each list to controller (thymeleaf Spring Boot)

我需要一些帮助才能将List产品作为参数发送到我的控制器。

<div class="panel-body">
    <div th:each="produto : ${produtos}" class="form-group" id="produtos">
       <label th:text="${produto.nome}" class="col-sm-2 control-label">
       </label>
       <div class="col-sm-1">
           <input type="text" class="form-control js-number" id="quantPedido" 
           th:field="*{produto.quantPedido}"/>
       </div>
    </div>
</div>

我的控制器

@RequestMapping(method = RequestMethod.POST)
public String salvar(@Validated Pedido pedido, List<Produto> produtos, Errors errors, RedirectAttributes attributes) {


}

非常感谢!

您需要将您的产品列表扭曲到一些bean中,然后将该bean发送到html并进行填充。 例:

豆角,扁豆:

public class Pedido{
    @Getter @Setter
    private List<Product> products = new ArrayList();
    //extra attributes
}

在您的控制器中:

ModelAndView mav = new ModelAndView("yourView");
Pedido pedido = new Pedido();
mav.addObject("pedido", pedido);

在您的html中:

<form th:object="${pedido}" th:action="@{/savePedido}" method="POST">
    //...
    <div th:each="produto : ${produtos}" class="form-group" id="produtos">
       <label th:text="${produto.nome}" class="col-sm-2 control-label">
       </label>
       <div class="col-sm-1">
           <input class="form-control js-number" id="quantPedido" th:field="*{products[__${produtoStat.index}__].quantPedido}"/>
       </div>
    </div>
    //...                                
</form>

暂无
暂无

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

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