【问题标题】:jstl c:foreach can the step be 0.5jstl c:foreach 步长可以为 0.5
【发布时间】:2015-09-16 03:06:07
【问题描述】:

我的jsp中有以下代码

    <c:forEach var="starCounter" begin="1" end="5" step="1">
        <c:if test="${starCounter le averageRating}">
                <i class="glyphicon glyphicon-star"></i>
        </c:if>
        <c:if test="${starCounter gt averageRating}">
                <i class="glyphicon glyphicon-star-empty"></i>
        </c:if>
    </c:forEach>

我想将 1 的步长更改为 0.5,但无法这样做,因为当我将步长更改为 0.5 时,出现以下错误并且我的 jsp 无法编译

Caused by: java.lang.NumberFormatException: For input string: "0.5"

正如this link 中提到的,似乎 step 必须 >= 1。

有什么方法可以做我想做的事吗?

感谢您的帮助。

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    正如doc 中提到的foreachstepint,它不能根据需要采用像0.5 这样的双精度/浮点值。所以IMO不可能,

    【讨论】:

      【解决方案2】:

      您可以通过使用 JSP 脚本实现此目的 :)

          <%
              for (double i = 0; i <= 5; i+=0.5) {
                  if (i < averageRating) {
          %>
                  <i class="glyphicon glyphicon-star"></i>
          <%
                  } else {
          %>
                  <i class="glyphicon glyphicon-star-empty"></i>
          <%
                  }
              }
          %>
      

      【讨论】:

        【解决方案3】:

        忘记添加我的最终答案。所以就在这里。我刚刚添加了另一个变量 starHalfStepCounter 并围绕它玩了。

        更新后的代码是

        <c:forEach var="starCounter" begin="1" end="5">
                <c:set var="starHalfStepCounter" value="${starCounter - 0.5}" />                                        
                <c:choose>
                    <c:when test="${starCounter le averageRating}">
                        <i class="glyphicon glyphicon-star"></i>
                    </c:when>                                       
                    <c:when test="${starCounter gt averageRating}">                                             
                        <c:choose>
                            <c:when test="${starHalfStepCounter le averageRating}">                                                     
                                <i class="glyphicon glyphicon-star half"></i>
                            </c:when>
                            <c:otherwise>
                                <i class="glyphicon glyphicon-star-empty"></i>
                            </c:otherwise>                                              
                        </c:choose>
                    </c:when>
                </c:choose>
            </c:forEach>
        

        【讨论】:

          【解决方案4】:

          遇到了同样的问题,想出了一个简单的解决方案,代码显示了从 12 到 36 的选择元素的选项创建,步长为 0.25:

          <select>
          <c:set value=".25" var="doubleStep" />
          <c:forEach begin="${12/doubleStep}" end="${36/doubleStep}" var="step">
              <option value="${step * doubleStep}">${step * doubleStep}</option>
          </c:forEach>
          </select>
          

          效果很好!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-09
            • 1970-01-01
            • 1970-01-01
            • 2011-04-22
            • 2020-08-23
            • 1970-01-01
            相关资源
            最近更新 更多