Java jstl c:foreach的步长可以是0.5吗
我的jsp中有以下代码Java jstl c:foreach的步长可以是0.5吗,java,jsp,jstl,Java,Jsp,Jstl,我的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 tes
<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
有什么方法可以实现我想要的目标吗
谢谢您的帮助。如for
foreach
中所述,步骤是int
,它不能根据需要采用双精度/浮点值,如0.5
。因此,我不可能,您可以通过使用JSP scriplets实现这一点:)
忘了添加我的最终答案。就是这样。我刚刚添加了另一个变量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的选项创建,步骤为.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>
${step*doubleStep}
效果很好 我也有同样的假设。我将尝试用其他方式来实现这一点。谢谢你的帮助help@BalwinderSingh欢迎
<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>
<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>