【问题标题】:Printing an unordered list that is in a ordered list by using a for each loop in Thymeleaf通过在 Thymeleaf 中使用 for each 循环打印有序列表中的无序列表
【发布时间】:2021-08-05 15:48:26
【问题描述】:

我正在处理一个需要我在有序列表中打印无序列表的项目。

我目前的代码只显示第一个列表项,如下所示:

  1. 姓氏
  2. 姓氏 等等……

标签 getThemePark 不为空,但由于某些奇怪的原因不会显示。

<ol>
    <th:block th:each="v : ${visitors}">
    <li th:text = "${v.getSurName()} + ' '+ ${v.getFirstName()}" th:with ="" >
        <ul>
            <li th:text = "${v} + ' '+ ${v.getThemeParkCode()} "></li>
        </ul>
    </li>
    </th:block>
</ol>

这是确保我可以在 thymeleaf html 中使用访问者的代码


@RequestMapping("/6_ShowAllVisitors")
    public String DisplayVisitors(Model model) {

        model.addAttribute("visitors", visitors);
        return "/6_ShowAllVisitors";
    }
    enter code here

如果有帮助,我的数组就是这样构建的。

private ArrayList<Visitor> fillVisitors() {
Visitor visitor4 = new Visitor("Name", "Surname");
        visitor4.setYearOfBirth(int year);
        visitors.add(visitor4);
        visitors.get(3).addToWishList("nameOfAttraction");
        visitors.get(3).addToWishList("nameOfAttraction");
...

这就是我启动 MainController.Java 的方式

    private ArrayList<Visitor> visitors;


    @PostConstruct
    private void fillData() {
        visitors = new ArrayList<>(fillVisitors());
    }

有关访客类的更多信息:

public class Visitor extends Person{
  private int yearOfBirth;
  private int themeParkCode;
  


    public Visitor(String firstName, String surName) {
        super(firstName, surName);
        wishList = new ArrayList<>();
        themeParkCode = -1;
        System.out.println();
    }

    public int getYearOfBirth() {
        return yearOfBirth;
    }

    public void setYearOfBirth(int yearOfBirth) {
        this.yearOfBirth = yearOfBirth;
    }

    public int getThemeParkCode() {
        return themeParkCode;
    }

    public void setThemeParkCode(int themeParkCode) {
        this.themeParkCode = themeParkCode;
    }

这是父类操作Visitor:

public class Person {
    private String firstName;
    private String surName;

    public Person(){}

    public Person(String firstName, String surName) {
        this.firstName = firstName;
        this.surName = surName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

提前致谢!

【问题讨论】:

  • 你能添加更多关于你的Visitor类的细节吗?
  • @msucil 刚刚添加了一堆我的代码,感谢反馈!

标签: java html list foreach thymeleaf


【解决方案1】:

使用th:text 会覆盖它出现的标记内的任何内容。模板的内容可能看起来像这样:

<ol>
  <li th:each="v : ${visitors}">
    <span th:text="${v.getSurName()} + ' '+ ${v.getFirstName()}" />
    <ul>
      <li th:text = "${v} + ' '+ ${v.getThemeParkCode()}"></li>
    </ul>
  </li>
</ol>

【讨论】:

  • 感谢您帮助我!这似乎有效:)
猜你喜欢
  • 2017-12-13
  • 2019-09-04
  • 2012-04-06
  • 2013-06-21
  • 2022-01-23
  • 2019-01-18
  • 2013-10-15
  • 2022-11-24
  • 1970-01-01
相关资源
最近更新 更多