超级无敌美少女x 2022-06-14 20:49 采纳率: 100%
浏览 159
已结题

thymeleaf 遍历 无法显示数据

问题遇到的现象和发生背景

写了个网页想测试我的服务器能不能正常返回数据,使用了thymeleaf和springboot,我现在想在网页端以表格的形式显示我的数据,但是一直加载不出来

问题相关代码,请勿粘贴截图

第一部分,controller的定义

@Controller
@RequestMapping("/news")
public class NewsController {
    private static Map<Integer,NewsAbs> newsListMap=null;
    @Autowired
    private NewsMapper newsMapper;
    @RequestMapping("/list")
    public String newsList(Model model){
        //测试1
        List<NewsAbs> newsAbsList=newsMapper.queryNewsAbsList();
        newsListMap=new HashMap<Integer,NewsAbs>();
        newsListMap.put(1,new NewsAbs(1,1,"aa"));
        newsListMap.put(2,new NewsAbs(2,1,"bb"));
        newsListMap.put(3,new NewsAbs(3,1,"cc"));
        Collection<NewsAbs> newsAbsCollection=newsListMap.values();
        model.addAttribute("newsAbsCollection",newsAbsCollection);
        String title=newsListMap.get(1).getTitle();//可以得到“aa"
        //最后测试
//        ArrayList<NewsAbs> newsAbs=null;
//        newsAbs.add(new NewsAbs(1,1,"aa"));
//        newsAbs.add(new NewsAbs(2,1,"bb"));
//        model.addAttribute("newsAbs",newsAbs);
        return "newshtml/list";
    }

第二部分list.html部分代码

                        <table class="table table-striped table-sm">
                            <thead>
                                <tr>
                                    <th>id</th>
                                    <th>title</th>
                                    <th>great</th>

                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="new:${newsAbsCollection}">
                                    <td th:text="${new.getId()}"></td>
                                    <td th:text="${new.getTitle()}"></td>
                                    <td th:text="${new.getGreat()}"></td>
                                </tr>
                            </tbody>
                        </table>
运行结果及报错内容
我的解答思路和尝试过的方法

开头确实有引入thymeleaf


```html

<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">

跟着狂神做的也可以成功显示,代码基本就是根据狂神的代码改的


###### 我想要达到的结果

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/897308012556135.png "#left")


![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/435818012556177.png "#left")
  • 写回答

2条回答 默认 最新

  • 太空眼睛 Java领域新星创作者 2022-06-19 15:16
    关注

    按如下方式修改代码即可
    把new改成new2

    如有帮助,请采纳,十分感谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月27日
  • 已采纳回答 6月19日
  • 赞助了问题酬金5元 6月14日
  • 创建了问题 6月14日

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料