如何在 Thymeleaf 中将列表发布到控制器 [英] How to post a list to controller in Thymeleaf

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

问题描述

我是 Thymeleaf 的新手,也许这是一个简单的问题.请帮助我.谢谢.

控制器代码:

@Controller公共类测试控制器{@GetMapping(value = "/")public String testget(Map model) {TestBean bean = new TestBean();列表<人>list = new ArrayList<>();for (int index = 1; index <5; index++) {人 p = 新人();p.setId(index);p.setName("name" + index);list.add(p);}model.put("allList", "nothing");bean.setList(list);model.put("testbean", bean);返回新文件";}@PostMapping(value = "/")public String testpost(Map模型//, @ModelAttribute(name = "testbean") TestBean bean) {列表<人>list = bean.getList();model.put("bean", bean);model.put("allList", list.toString());返回新文件";}}

简单的映射器和一个 Person bean:

@Data公共类 TestBean {私人名单<人>清单;}@数据公共类人{私有整数 ID;私人字符串名称;}

HTML 代码:

<p th:text="'ALL :' + ${allList}"></p><表格><tr th:each="person : ${testbean.list}"><td>Id:<input type="text" th:value="${person.id}"/></td><td>name: <input type="text" th:value="${person.name}"/></td></tr><input type="submit" value="submit"/></表单>

我想将列表放到页面并更改属性以刷新.但我不知道如何添加 th:field 标签,我尝试添加

**th:field="*{testbean.list[__${index}__].id}"**

但它失败了:

bean 类 [com.TestBean] 的无效属性testbean"

<块引用>

更新1

我试过了

th:field="*{list[__${index}__].id}"

我在添加 th:field

的地方出错

Servlet.service() for servlet [dispatcherServlet] in context with path [] 抛出异常 [Request processing failed;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (NewFile:14)] 与根本原因java.lang.NumberFormatException:对于输入字符串:null"

我的问题是我可以做什么才能在控制器中获得列表.

解决方案

bean 类 [com.TestBean] 的无效属性testbean"

使用 th:field="*{list[__${index}__].id} 模式而不使用 testbean 定义.如果你使用 * 符号,然后它将引用父对象.

<块引用>

java.lang.NumberFormatException:对于输入字符串:null"

向循环中添加诸如rowStat"之类的任何变量以保持状态索引:

th:each="person,rowStat : ${testbean.list}"

并像 th:field="*{list[__${rowStat.index}__].id} 一样访问它.因此索引不会为空并且将字符串转换为 int 没有问题.

I am new to Thymeleaf,Maybe this is a simple question.Please help me.Thanks.

Controller code:

@Controller
public class TestController {

@GetMapping(value = "/")
public String testget(Map<String, Object> model) {
    TestBean bean = new TestBean();
    List<Person> list = new ArrayList<>();
    for (int index = 1; index < 5; index++) {
        Person p = new Person();
        p.setId(index);
        p.setName("name" + index);
        list.add(p);
    }
    model.put("allList", "nothing");
    bean.setList(list);
    model.put("testbean", bean);
    return "NewFile";
}

@PostMapping(value = "/")
public String testpost(Map<String, Object> model//
        , @ModelAttribute(name = "testbean") TestBean bean) {
    List<Person> list = bean.getList();
    model.put("bean", bean);
    model.put("allList", list.toString());
    return "NewFile";
}}

simple mapper and a Person bean:

@Data
public class TestBean {

   private List<Person> list;

}

@Data
public class Person {
   private int id;
   private String name;
}

HTML code :

<form action="#" th:action="@{/}" th:object="${testbean}" method="post">
    <p th:text="'ALL : ' + ${allList}"></p>
    <table>
        <tr th:each="person : ${testbean.list}">
            <td>Id:<input type="text" th:value="${person.id}" 
             /></td>
            <td>name: <input type="text" th:value="${person.name}" /></td>
        </tr>
    </table>
    <input type="submit" value="submit" />
</form>

I want put list to page and change properties to refresh. but i don't know how to add th:field tag, I try to add

**th:field="*{testbean.list[__${index}__].id}"** 

but it failed with :

Invalid property 'testbean' of bean class [com.TestBean]

UPDATE1

And i tried

th:field="*{list[__${index}__].id}"

I got a error Where I added th:field

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (NewFile:14)] with root cause
java.lang.NumberFormatException: For input string: "null"

My questions is what i can do something that i can get a List in controller.

解决方案

Invalid property 'testbean' of bean class [com.TestBean]

Use th:field="*{list[__${index}__].id} pattern without testbean definition. If you use * notation then it will reference to parent object.

java.lang.NumberFormatException: For input string: "null"

Add any variable like "rowStat" to the loop for keeping status index:

th:each="person,rowStat : ${testbean.list}"

and access it like th:field="*{list[__${rowStat.index}__].id}. So index will not be null and no problem converting the string to int.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆