微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过在 Thymeleaf 中使用 for each 循环打印有序列表中的无序列表

如何解决通过在 Thymeleaf 中使用 for each 循环打印有序列表中的无序列表

我正在做一个项目,需要我在有序列表中打印无序列表。

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

  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;
    }

这是访问者的父类操作:

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;
    }

提前致谢!

解决方法

使用 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。