如何使用 thymeleaf 在列表中添加对象? [英] How to add an object in list using thymeleaf?

查看:113
本文介绍了如何使用 thymeleaf 在列表中添加对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在会话中有一个对象,例如一个部门,这个部门有孩子.我得到了它的孩子的列表,现在我想在这个列表中添加这个部门对象.这在服务器端非常简单,但它是可以在 thymeleaf 中做到这一点.

解决方案

是的,可以在 Thymeleaf 中完成,因为它使用 对象图导航语言 用于评估表达式值 - ${...} 块的内容.您可以正常访问模型对象上的公共方法,因此调用 boolean List#add(E e) 方法有效.

但正如其他人提到的这不是一个好主意,您应该重新设计您的应用程序以在控制器中填充您的模型.另一方面,如果您需要进行一些讨厌的黑客攻击或只是玩玩,请参见下面的示例.它使用 th:text 来评估添加到列表,然后使用 th:remove 来清理标签并且不留下任何痕迹.

部门模型类:

公开课系{私人字符串名称;公共部门(字符串名称){this.name = 名称;}公共字符串 getName() {返回名称;}公共无效集名称(字符串名称){this.name = 名称;}}

模型中的数据(例如会话)

列表<部门>孩子 = 新的 ArrayList<>();children.add(new Department("Department 1"));children.add(new Department("Department 2"));//在模型中作为*部门*变量部门部门=新部门(主要部门");部门.setChildren(儿童);

Thymeleaf 模板:

<html xmlns:th="http://www.thymeleaf.org"><头><身体><h1>之前</h1><ul><li th:each="child : ${department.children}" th:text="${child.name}"></li><!--/* 一个讨厌的把戏来了!*/--><div th:text="${department.children.add(department)}" th:remove="all"></div><h1>结果</h1><ul><li th:each="child : ${department.children}" th:text="${child.name}"></li></html>

<块引用>

之前

  • 部门 1
  • 部门 2

结果

  • 部门 1
  • 部门 2
  • 主要部门

PS Spring 表达式语言 与 Spring 集成一起使用,但对于本示例,它没有区别.

I have an object in session for example of a department and this department have children.I got the list of its children, now i want to add this department object in this list.This is very simple at server side but is it possible to do this in thymeleaf.

解决方案

Yes it is possible to do it in Thymeleaf since it's using Object-Graph Navigation Language for evaluating expression values - content of ${...} block. You can access public methods normally on your model object so calling boolean List#add(E e) method works.

But as others mentioned it's NOT really good idea and you should redesign your application to fill your model in controller. On the other side if you need to make some nasty hack or just playing around see example below. It's using th:text to evaluate adding to list then th:remove to clean tag and leave no trace.

Department Model class:

public class Department {

    private String name;

    public Department(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Data in model (eg. session)

List<Department> children = new ArrayList<>();
children.add(new Department("Department 1"));
children.add(new Department("Department 2"));

// In model as *department* variable
Department department = new Department("Main Department");
department.setChildren(children);

Thymeleaf template:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
    <body>
        <h1>Before</h1>
        <ul>
           <li th:each="child : ${department.children}" th:text="${child.name}"></li>
        </ul>

        <!--/* Here comes a nasty trick! */-->
        <div th:text="${department.children.add(department)}" th:remove="all"></div>

        <h1>Result</h1>
        <ul>
           <li th:each="child : ${department.children}" th:text="${child.name}"></li>
        </ul>
    </body>
</html>

Before

  • Department 1
  • Department 2

Result

  • Department 1
  • Department 2
  • Main Department

P.S. Spring Expression Language is used with Spring integration but for this example it makes no difference.

这篇关于如何使用 thymeleaf 在列表中添加对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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