Thymeleaf:如何使用具有可变限制的#numbers.sequence()? [英] Thymeleaf: How to use #numbers.sequence() with a variable limit?
问题描述
我正在使用 thymeleaf 和 Spring Boot 从数据库中调用一个 int 值来确定应在我的 html 中显示的星数,但使用 ${#numbers.sequence(1,obj.stars)} 则不会'好像没用.
I'm calling an int value from a database to determine the number of stars that should be displayed in my html using thymeleaf and Spring Boot, but using ${#numbers.sequence(1,obj.stars)} doesn't seem to work.
这是我的 html-thymeleaf 代码:
this is my html-thymeleaf code:
<tr th:each="obj : ${allObjs}" class="pointer" th:onclick="'javascript:openobj(\'' + ${obj.id} + '\');'">
<td class="text-center" th:text="${obj.id}"></td>
<td class="text-center" th:text="${obj.code}"></td>
<td class="text-center" th:text="${obj.name}"></td>
<td class="text-center" th:text="${obj.contract}"></td>
<td class="text-center" th:text="${obj.difficulty}"></td>
<td class="text-center" th:text="${obj.priority}"></td>
<td class="text-center">
<!--this is the line I can't get to work :(-->
<span class="fa fa-star-o" th:each="star:${#numbers.sequence(1,obj.stars)}"></span>
</td>
<td class="text-center" th:text="${obj.state}"></td>
<td class="text-center" th:text="${obj.percent}"></td>
<td class="text-center" th:text="${obj.term}"></td>
<td class="text-center" th:text="${obj.version}"></td>
<td class="text-center" th:text="${obj.price}"></td>
</tr>
和我的控制器
@GetMapping("/Obj")
public ModelAndView index() {
ModelAndView view = new ModelAndView("/Obj/index");
view.addObject("title", "Obj");
List<Obj> allObjs = ObjService.findAll();
view.addObject("allObjs", allObjs);
return view;
}
推荐答案
好吧,我知道回答你自己的问题很奇怪,但感谢 Michael Petch 测试了它,我发现问题出在顺序上.当 obj.stars 中的值为 0 时,它从 1 开始,因此无法以 1 的步长创建序列.
Well, I know it's weird to answer your own question but, thanks to Michael Petch who tested it, I found that the problem was in the sequence. It was starting from 1 when I had values of 0 in obj.stars so the sequence couldn't be created with a step of 1.
改成
<span class="fa fa-star-o" th:each="star:${#numbers.sequence(0,obj.stars)}"></span>
解决了问题.
这篇关于Thymeleaf:如何使用具有可变限制的#numbers.sequence()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!