Thymeleaf

thymeleaf怎么获取model的值

小亿
354
2024-01-09 19:26:28
栏目: 编程语言

在Thymeleaf中,可以通过使用${}表达式来获取model中的值。下面是一些示例:

  1. 获取字符串值:

    <span th:text="${modelValue}"></span>

    这将在<span>标签中显示model中名为modelValue的字符串值。

  2. 获取对象属性值:

    <span th:text="${modelObject.property}"></span>

    这将在<span>标签中显示model中名为modelObject的对象的property属性值。

  3. 使用Thymeleaf内置的迭代变量来遍历集合:

    <ul>
        <li th:each="item : ${modelList}" th:text="${item}"></li>
    </ul>
    

    这将在一个无序列表中显示model中名为modelList的集合中的每个元素。

注意:在使用Thymeleaf获取model的值时,需要确保model中的值已经在controller中设置好了。

1
看了该问题的人还看了