Thymeleaf:如何使用带有变量限制的#numbers.sequence()?

2023-12-30

我正在使用 thymeleaf 和 Spring Boot 从数据库调用 int 值来确定应在 html 中显示的星星数量,但使用 ${#numbers.sequence(1,obj.stars)} 似乎并不工作。

这是我的 html-thymeleaf 代码:

    <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 创建序列。

将其更改为

<span class="fa fa-star-o" th:each="star:${#numbers.sequence(0,obj.stars)}"></span> 

解决了问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Thymeleaf:如何使用带有变量限制的#numbers.sequence()? 的相关文章

随机推荐