kpnkpbb 2022-05-28 22:25 采纳率: 100%
浏览 445
已结题

用thymeleaf的th:each循环遍历不出来

🆘🆘什么数据都显示不出来,控制台也打印不出来,感觉是因为前后没连一起,但是不知道怎么改。
有没有人帮帮我。😭🙏😿🙏

img

img

img

  • 写回答

1条回答 默认 最新

  • okokabcd 2022-05-29 01:41
    关注

    列一个能正确显示的操作,你可以对接看一下哪个环节出问题了
    配置,application.yml

    # thymeleaf 静态资源配置
    spring:
      thymeleaf:
        prefix: classpath:/templates/
        encoding: UTF-8
        suffix: .html
        mode: HTML5
        # 关闭缓存, 即时刷新, 上线生产环境需要改为true
        cache: false
        servlet:
          content-type: text/html
    

    后端

    package com.example.thymeleaf.controller;
    
    import com.example.thymeleaf.entity.OrderItem;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpSession;
    import java.util.ArrayList;
    import java.util.List;
    
    @Controller
    @RequestMapping("/demo")
    public class Demo01Controller {
        @RequestMapping("/cart")
        public String listOrders(Model model, HttpSession session) {
            System.out.println("demo/cart");
            model.addAttribute("orders", getOrderItems());
            return "mycart";
        }
    
        private List<OrderItem> getOrderItems() {
            List<OrderItem> list = new ArrayList<>();
            list.add(new OrderItem("1001", "订单1", "文件1"));
            list.add(new OrderItem("1002", "订单2", "文件2"));
            list.add(new OrderItem("1003", "订单3", "文件3"));
            return list;
        }
    }
    

    前端

        <table class="layui-table">
            <thead>
            <tr>
                <th>序号</th>
                <th>订单号</th>
                <th>标题</th>
                <th>文件名</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="item:${orders}">
                <td th:text="${itemStat.index+1}"></td>
                <td th:text="${item.orderId}"></td>
                <td th:text="${item.title}"></td>
                <td th:text="${item.fileName}"></td>
            </tr>
            </tbody>
        </table>
    

    前台显示效果

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月6日
  • 已采纳回答 5月29日
  • 创建了问题 5月28日

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示