在thymeleaf循环 [英] for loop in thymeleaf

查看:175
本文介绍了在thymeleaf循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能做到以下几点(java):

$ p $ = 20){
//应循环5次!

在Thymeleaf?

我试过这个:

 < option th:each =i:$ {#numbers.sequence(1 ,81/20)}> 
< p th:text =$ {i}>< / p> <! - THIS循环4次,而不是5次 - >
< / option>

问题在于它不如java代码片段准确。如何做到这一点?

解决方案

我假设这是由于您使用的数字。对于你的java代码,
int i = 0;我< 81; i + = 20
会返回i = 0,i = 20,i = 40,i = 60和i = 80

然而,您的以下代码
numbers.sequence(1,81/20)}
应该返回从1到4.05的整数,分别为1,2,3和4.

第一个循环返回5个结果,因此运行5次。
第二次只返回4个结果,所以运行4次。我建议从0开始运行你的序列,根据需要返回5个结果。


如果你想让你的java代码来镜像第二个代码,你应该把它改为:
int i = 1;我< 4.05;简单地说,你正在运行一个不同数字的循环,我建议将第二条语句从0开始改变。

$ b

$ b

How can I do the following (java):

for(int i = 0; i < 81 ; i+=20){
   //Should loop through 5 times!
}

in Thymeleaf?

I've tried this:

<option th:each="i : ${#numbers.sequence( 1, 81/20)}">
   <p th:text="${ i }"></p> <!-- THIS loops 4 times, instead of 5 -->
</option>

The problem is that it is not as accurate as the java piece of code. How to do this?

解决方案

I am assuming this is due to the numbers you are using. For your java code, int i = 0; i < 81 ; i+=20 will return i=0, i=20, i=40, i=60 and i=80

however your following code numbers.sequence( 1, 81/20)} should returns the integers from 1 to 4.05, being 1, 2, 3, and 4.

The first loop returns 5 results for i, therefore runs 5 times. the second returns only 4 results, so runs 4 times. I would suggest running your sequence starting at 0 to return 5 results as desired.

If you wanted your java code to mirror the second code, you should change it to: int i = 1; i < 4.05 ; i+=1

To put it simply, you are running through a loop with different numbers, I suggest changing the second statement to start from 0.

这篇关于在thymeleaf循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆