开发者

Jsp and ?: not working as expected

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜