开发者

Jsp and ?: not working as expected

开发者 https://www.devze.com 2023-01-19 21:25 出处:网络
I have this code: <c:forEach var=\"product\" items=\"${products}\" begin=\"${begin}\" end=\"${end}\" varStatus=\"loopStatus\" step=\"1\">

I have this code:

<c:forEach var="product" items="${products}" begin="${begin}" end="${end}" varStatus="loopStatus" step="1">
    <div class="home_app "${loopStatus.index % 2 == 0 ? '' : 'white_bg'}">
开发者_开发百科

When I browse to the jsp I am getting this in the div:

<div }="" white_bg="" :="" ?="" 0="" 2="=" %="" ${loopstatus.index="" class="home_app ">


Try this (change in bold):

<c:forEach var="product"
           items="${products}"
           begin="${begin}"
           end="${end}"
           varStatus="loopStatus"
           step="1">
    <div class="${loopStatus.index % 2 == 0 ? '' : 'white_bg'}">

My personal preference is the following instead of the ?: operator:

<c:choose>
    <c:when test="${(loopStatus.index % 2) == 1}">
        <div>
    </c:when>
    <c:otherwise>
        <div class="white_bg">
    </c:otherwise>
</c:choose>


The " before the dollar sign seems to be at the wrong place. Remove it.


The conditional operator (and EL in template text) was introduced in JSP 2.0. Chances are that you're running a servletconainer which doesn't support JSP 2.0 or is declaring web.xml as Servlet 2.2 or older.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号