Search code examples
javajspjstl

jstl c:foreach can the step be 0.5


I have following code in my 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>

I want to change the step for 1 to 0.5 but unable to do so cause when I change the step to 0.5, I get the following error and my jsp doesn't compile

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

As mentioned in this link, it seems like step must be >= 1.

Is there any way to do what I want to achieve?

Thanks for your help.


Solution

  • Forgot to add my final answer. So here it is. I just added another variable starHalfStepCounter and played around it.

    The updated code is

    <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>