将列表对象从 thymeleaf 发送到控制器 [英] Send list object from thymeleaf to controller

查看:48
本文介绍了将列表对象从 thymeleaf 发送到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的课

公共类 ListLoaiDoan {私有 ArrayList贷款;//获取器设置器}

我的控制器将列表对象从控制器绑定到百里香

@RequestMapping("/luunhieuobject")公共字符串 LoadNhieuObjectCungLuc(模型模型){ListLoaiDoan listLoaiDoanAAA = new ListLoaiDoan();model.addAttribute("listLoaiDoan111",listLoaiDoanAAA);return "/MHtrangchu/LuuNhieuObjCungLuc";}//这是从百里香叶到控制器的方法保存列表对象@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

<!--输入字段--><div class="row"><div class="col"><div id="电影列表"><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 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 class="col-lg-6 form-group"><input th:field="*{loaiDoans[__${stat.index}__].madoan}" type="text" class="form-control"/>

<!--添加新行按钮--><div class="row"><div class="col"><button type="button" class="btn btn-success" onclick="addRow()">添加行</button>

<!--提交表单按钮--><div class="row text-right"><div class="col"><button type="submit" class="btn btn-primary">提交</button>

</表单>

那不是在屏幕上显示annything,我知道因为listLoaiDoanAAA"是空的,而thymeleaf中的th:each"没有什么可显示的,如何生成input"标签并保存到控制器帮助我!

解决方案

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

我像这样修复我的控制器

@GetMapping("/luunhieuobject")公共字符串 LoadNhieuObjectCungLuc(模型模型){ListLoaiDoan listLoaiDoanAAA = new ListLoaiDoan();ArrayListLDD = new ArrayList(10);listLoaiDoanAAA.setLoaiDoans(LDD);model.addAttribute("listLoaiDoan111", listLoaiDoanAAA);return "/MHtrangchu/LuuNhieuObjCungLuc";}

I have a big problem about saving an Object list from Thymeleaf to the controller. Object List in thymeleaf is generated by Jquery. but I don't know how to get the data to Controller, that Object list doesn't know the size. Because users can add it anytime. Please help me to send a list object in thymeleaf to controller.

I’ve Created a new class with 1 properties: ArrayList loaiDoans; "LoaiDoan" is a Object that i want to save. And using that class is an object to Save List "LoaiDoan" from thymeleaf to controller. But List don't know the size first.because that genarated in thymeleaf. The first time i load the Model, the Model contain List is empty,so that list is not display in screen.

This is my class

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

My controller bind list object from controller to 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>

That not display annything in the screen, i know that because "listLoaiDoanAAA" is the empty and "th:each" in thymeleaf have nothing to show, how to generate "input" tag and save to controller help me !

解决方案

I solved this problem ! Set a size for ArrayList before bind it to thymeleaf ! I save my day. thanks Stackoverflow.

i fix my controller like this

@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";
        }

这篇关于将列表对象从 thymeleaf 发送到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆