jstl c:foreach step可以是0.5吗

jstl c:foreach can the step be 0.5

我的 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中所述,步骤似乎必须 >= 1。

有什么方法可以达到我想要的效果吗?

感谢您的帮助。

foreachdoc 中所述,stepint 并且它不能像 0.5 那样采用 double/float 值你需要。所以 IMO 不可能,

您可以使用 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>
    <%
            }
        }
    %>

忘记添加我的最终答案。所以就在这里。我刚刚添加了另一个变量 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>

运行 遇到同样的问题,提出了一个简单的解决方案,代码显示了从 12 到 36 的 select 元素的选项创建,步长为 .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>

效果很好!