【问题标题】:Spring thymeleaf doesn't iterate over listSpring thymeleaf 不会遍历列表
【发布时间】:2019-12-24 11:21:02
【问题描述】:

我正在尝试遍历 List 并在表格中显示结果,但表格仍为空。

控制器

@RequestMapping(value="/home")
    public String home(Model model) throws IOException {
        List<OrderNotify> notifications = new ArrayList<OrderNotify>();
        for (int i = 0; i < 10; i++) {
            OrderNotify notification = new OrderNotify("1", "2", "3", "4");
            notifications.add(notification);
            

        }
            model.addAttribute("notifications", notifications);
            return "home";
    }

home.jsp - 表格

<table id="handle" style=" margin:auto">

    <tr>
        <th>Bestellnummer</th>
        <th>Datum</th>
        <th>Status</th>
        <th>Handlung erforderlich</th>
    </tr>
    <tr th:each="notify : ${notifications}">
        <td th:text="${notify.orderid}"></td>
        <td th:text="${notify.date}"></td>
        <td th:text="${notify.status}"></td>
        <td th:text="${notify.handle}"> </td>
    </tr>



</table>

OrderNotify.java

public class OrderNotify {

    public String orderid;
    public String date;
    public String status;
    public String handle;

    public OrderNotify(String orderid, String date, String status, String handle) {
        this.orderid = orderid;
        this.date = date;
        this.status = status;
        this.handle = handle;
    }

    public List<String> getAll(){
        return null;
    }

    public String getOrderid() {
        return orderid;
    }

    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getHandle() {
        return handle;
    }

    public void setHandle(String handle) {
        this.handle = handle;
    }
}

我预计确实会看到 10 行具有与列表的 OrderNotify 对象相同的信息,但它是空的。我在浏览器中查看了源代码,结果如下:

  <tr th:each="notify : [com.example.spring.utils.OrderNotify@42b3c931, com.example.spring.utils.OrderNotify@76af8a32, com.example.spring.utils.OrderNotify@28025a03, com.example.spring.utils.OrderNotify@5e6eadf0, com.example.spring.utils.OrderNotify@2481d4d, com.example.spring.utils.OrderNotify@83ce92c, com.example.spring.utils.OrderNotify@3254786a, com.example.spring.utils.OrderNotify@197e42fd, com.example.spring.utils.OrderNotify@5b1e86ea, com.example.spring.utils.OrderNotify@3484712c]">
        <td th:text=""></td>
        <td th:text=""></td>
        <td th:text=""></td>
        <td th:text=""> </td>
    </tr>

【问题讨论】:

  • 无法重现该问题。首先浏览器不应该显示th:each="notify : [com.example等。视图解析器会解决这些问题。你有 thymeleaf spring boot starter 依赖吗?您的应用程序中是否配置了视图解析器?
  • 似乎缺少 thymeleaf 依赖项。
  • 您能否更新您的问题以包括您的 pom.xml?

标签: java spring loops model-view-controller thymeleaf


【解决方案1】:

Thymeleaf 和 JSP 是两个不同的服务器端渲染模板。如果没有配置,您不能同时使用它们。

根据您的代码,您应该更新使用 Thymeleaf 的设置:
一种。将 home.jsp 更改为 home.html
湾。将 home.html 移动到路径:src/main/resources/templates
C。在 application.properties 中配置 Thymeleaf:

   # Thymeleaf
   spring.thymeleaf.prefix=classpath:/templates/
   spring.thymeleaf.suffix=.html
   spring.thymeleaf.encoding=UTF-8
   spring.thymeleaf.mode=HTML5
   spring.thymeleaf.cache=false

d。在 pom.xml 中配置 Thymeleaf 依赖

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


对于查看模板,SpringBoot 建议使用 Thymeleaf 而不是 JSP,详见more

在 SpringBoot 应用中同时使用 Thymeleaf 和 JSP,需要进行更多配置。以下文章供您参考:
Using both Thymeleaf and JSP
https://www.oodlestechnologies.com/blogs/Use-Thymeleaf-And-JSP-Simultaneously-In-Spring-Boot-App/

【讨论】:

    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2018-09-24
    • 2020-05-27
    • 2017-10-18
    相关资源
    最近更新 更多