首页 > 解决方案 > 将列表对象从 thymeleaf 发送到控制器

问题描述

我在将对象列表从 Thymeleaf 保存到控制器时遇到了一个大问题。thymeleaf 中的对象列表由 Jquery 生成。但我不知道如何将数据获取到控制器,该对象列表不知道大小。因为用户可以随时添加。请帮助我将 thymeleaf 中的列表对象发送到控制器。

我创建了一个具有 1 个属性的新类: ArrayList loaiDoans; “LoaiDoan”是我要保存的对象。并且使用该类是将列表“LoaiDoan”从百里香保存到控制器的对象。但是 List 首先不知道大小。因为它是在百里香中产生的。我第一次加载模型时,模型包含列表是空的,因此该列表不会显示在屏幕上。

这是我的课

public class ListLoaiDoan {
    private ArrayList<LoaiDoan> loaiDoans;
//Getter Setter
}

我的控制器将列表对象从控制器绑定到 thymeleaf

@RequestMapping("/luunhieuobject")
public String LoadNhieuObjectCungLuc(Model model) {
    ListLoaiDoan listLoaiDoanAAA = new ListLoaiDoan();
    model.addAttribute("listLoaiDoan111",listLoaiDoanAAA);
            return "/MHtrangchu/LuuNhieuObjCungLuc";
        }
//This is the method save list Object from thymeleaf to controller
@PostMapping("/luunhieuobject")
public String processQuery(@ModelAttribute("listLoaiDoan111") ListLoaiDoan listLoaiDoan) {
System.out.println(listLoaiDoan.getLoaiDoans() != null ? listLoaiDoan.getLoaiDoans().size() : "List Empty");
              System.out.println("--");
              return "/MHtrangchu/LuuNhieuObjCungLuc";
   }

LuuNhieuObjCungLuc.html

<form th:object="${listLoaiDoan111}" method="post" th:action="@{/luunhieuobject}">

<!--INPUT FIELDS-->
        <div class="row">
            <div class="col">
                <div id="movieList">
                    <div class="row">
                        <div style="margin-left:100px;" class="col-4 form-group">tenloaidoan</div>
                        <div style="margin-left:100px;" class="col-4 form-group">madoan</div>
                    </div>
                    <div class="row item" th:each="row, stat : ${listLoaiDoan111.loaiDoans}">

                        <div class="col-lg-6 form-group">
                            <input th:field="*{loaiDoans[__${stat.index}__].tenloaidoan}" type="text" class="form-control"/>
                        </div>
                        <div class="col-lg-6 form-group">
                            <input th:field="*{loaiDoans[__${stat.index}__].madoan}" type="text" class="form-control"/>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<!--ADD NEW ROW BUTTON-->
        <div class="row">
            <div class="col">
                <button type="button" class="btn btn-success" onclick="addRow()">Add row</button>
            </div>
        </div>
        <!--SUBMIT FORM BUTTON-->
        <div class="row text-right">
            <div class="col">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
     </form>

那没有在屏幕上显示任何东西,我知道因为“listLoaiDoanAAA”是空的,而百里香中的“th:each”没有什么可显示的,如何生成“输入”标签并保存到控制器帮助我!

标签: spring-mvcthymeleaf

解决方案


我解决了这个问题!在将 ArrayList 绑定到 thymeleaf 之前设置它的大小!我节省了我的一天。感谢 Stackoverflow。

我像这样修复我的控制器

@GetMapping("/luunhieuobject")
        public String LoadNhieuObjectCungLuc(Model model) {
            ListLoaiDoan listLoaiDoanAAA = new ListLoaiDoan();
            ArrayList<LoaiDoan> LDD = new ArrayList<LoaiDoan>(10);
            listLoaiDoanAAA.setLoaiDoans(LDD);
            model.addAttribute("listLoaiDoan111", listLoaiDoanAAA);
            return "/MHtrangchu/LuuNhieuObjCungLuc";
        }

推荐阅读