【问题标题】:Need to access the simple list in Thymleaf需要访问 Thymeleaf 中的简单列表
【发布时间】:2021-07-25 17:25:25
【问题描述】:

控制器:

ArrayList<String> al = new ArrayList<String>(); 
            
while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
    model.addAttribute("result", al.add(inputLine));

HTML:

<ul th:each="al: ${result}">
    <li th:text="${al}"></li>

需要html中的所有附加值。但它返回真。有没有办法获取所有值?

【问题讨论】:

    标签: java html spring-boot web-services thymeleaf


    【解决方案1】:

    您将ArrayList#add(..) 方法的结果添加为模型属性,此方法不返回数组列表本身,而是一个布尔值。

    您应该添加数组列表本身:

    List<String> al = new ArrayList<>();
    while ((inputLine = in.readLine()) != null) {
      System.out.println(inputLine);
      al.add(inputLine);
    }
    
    model.addAttribute("result", al);
    

    【讨论】:

    • 很高兴它有帮助!请使用复选标记将问题标记为已回答,以便其他人知道已找到解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多