【问题标题】:How can i loop over an ArrayList using thymeleaf & spring Boot?如何使用 thymeleaf 和 spring Boot 遍历 ArrayList?
【发布时间】:2017-10-18 05:26:18
【问题描述】:

我是 thymeleaf 的新手,我尝试遍历一个 ArrayList,但它对我不起作用..请帮助: 这是我的 HTML 页面:

<body>
    <div class="row">
        <table>
            <tr th:each="data: mois">  
                <td class="text-center" th:text="${data}">data</td>
            </tr>			
        </table>
    </div>
</body>

这是我的控制器

@RequestMapping(value="/editePlanning", method= RequestMethod.GET)
    public String editePlanning(Model model){
        Psi psi = psiRepository.findOne((long) 1);
        List<String> data = new ArrayList<String>();

        for(int i=0;i<psi.getNombreMois();i++){
            int val = psi.getMoisDebut()+i%12;
            data.add(""+ val);
        }

        model.addAttribute("mois",data);
		
        return "editePlanning";
    }

【问题讨论】:

    标签: html spring-boot thymeleaf


    【解决方案1】:

    您的迭代中有错字(请参阅the docs,它们非常好):

      <tr th:each="data: ${mois}">
    

    别忘了你可以得到iteration index,用于生成元素的id

      <tr th:each="data, iterstat: ${mois}">
         <td th:text="${data}" th:id="|td${iterstat.index}|"></td>
      </tr>
    

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 1970-01-01
      • 2021-08-27
      • 2020-11-11
      • 1970-01-01
      • 2019-12-24
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多