【问题标题】:Thymeleaf: How to use #numbers.sequence() with a variable limit?Thymeleaf:如何使用具有可变限制的#numbers.sequence()?
【发布时间】:2017-04-30 04:24:06
【问题描述】:

我正在从数据库中调用一个 int 值,以确定应该使用 thymeleaf 和 Spring Boot 在我的 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;
 }

【问题讨论】:

  • 您遇到的错误是什么?百里香代码是正确的(刚刚测试过)。您是否验证了视图源,并确保它与您的 css 无关?
  • 错误是这样的:出现意外错误(类型=内部服务器错误,状态=500)。评估 SpringEL 表达式的异常:“#numbers.sequence(1,obj.stars)”(/Obj/index)
  • obj.stars 是否解析为整数?
  • jum,也许这就是问题所在。我不知道如何让 #numbers.sequence 将 obj.stars 视为整数
  • stars 被定义为私有 int 星星;在实体中

标签: html spring spring-boot thymeleaf


【解决方案1】:

嗯,我知道回答你自己的问题很奇怪,但是感谢测试它的 Michael Petch,我发现问题出在序列上。当我在 obj.stars 中的值为 0 时,它从 1 开始,因此无法以 1 的步长创建序列。

改成

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

解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2017-12-01
    • 2020-10-01
    • 2013-03-30
    相关资源
    最近更新 更多