【问题标题】:Thymeleaf th:each not working with hrefThymeleaf th:每个都不能使用 href
【发布时间】:2018-10-22 22:42:16
【问题描述】:

我是 Thymeleaf 的新手。我无法使用 th:each 显示数据

我想显示这些数据

categoryDaoImpl.java

static {
        Category category=new Category();

        category.setId(1);
        category.setName("Television");
        category.setDescription("This is first Television in our home.");
        category.setImageURL("rashed.jpg");
        category.setActive(true);

        categories.add(category);
}

在我的控制器 mycontroller.java 中

@RequestMapping(value= {"/","/home","/index"})
public String showHome(Model model) {
    model.addAttribute("categories",categoryDao.list());
    return "home";
}

这是我要显示数据的 html 页面。

<div class="list-group">
    <a th:each="category : ${categories}" th:text="${category}" th:href="#" class="list-group-item active">Home</a>
</div>

更多信息,那些html代码没有显示在

页面来源

我也尝试了这个解决方案 thymeleaf - combined th:each with th:href 。 但它在我的 html 文件中不起作用,我也在使用 bootstrap 4。我该如何解决这个问题? 提前致谢。

【问题讨论】:

  • 发生了什么?
  • 它不显示任何类别附带的数据。
  • 可能category 为空。最好也包含生成的 html。如果您不提供输入输出和预期输出等最少信息量,我们如何为您提供帮助。
  • 看看我更新的问题,还有一个想法是,那些代码没有显示在“检查”或“页面源”中,这意味着至少不能在浏览器中工作,如何解决这个问题?
  • 可以把categorydaoimpl类中list函数的代码贴出来吗?

标签: spring-mvc thymeleaf


【解决方案1】:

您在 html 代码中遇到的一个错误是,在 th:text 中您应该有 Description 属性(很可能)而不是对象类别 所以你需要以下内容

<div class="list-group">
    <a th:each="category : ${categories}" th:text="${category.description}" th:href="#" class="list-group-item active">Home</a>
</div>

【讨论】:

  • 我之前尝试过这种方法,但没有奏效。还有一个信息,当我打开“页面源代码”时,不显示任何代码,这意味着这些代码不能正常工作。我假设,如果这些 html 代码显示然后在我的项目中正常工作,但这里没有发生任何事情。
猜你喜欢
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 2015-11-19
  • 2019-10-17
相关资源
最近更新 更多