如果使用jstl循环if if循环如何使用

时间:2014-06-02 11:07:56

标签: spring jstl

我有一个要求,如果status为0,那么添加一个css类active,否则添加一个css类active1,为此我做了类似关注

<c:forEach items="${parentList}" var="test">

<c:choose>
  <c:when test="${test[1] eq 0}">
   <li><a href="#" class="active" value="${test[0].id}-${test[0].category}" id="${parent.id}" onclick="return getQuestions(this);" > 
${test[0].name }</a></li>
  </c:when>

  <c:otherwise>
  <li><a href="#" class="active1" value="${test[0].id}-${test[0].category}" id="${parent.id}" onclick="return getQuestions(this);" > 
<img src="${pageContext.request.contextPath}/resources/ui_resources/img/checked.jpg" alt="" height="20" width="20"/>${test[0].name }</a></li>
  </c:otherwise>
</c:choose>
</c:forEach>

现在我的要求

${test[0].category}是客户端,然后添加按钮<button name="delete/>

所以现在出现了4个条件

  1. 如果status为0且category不是client,则添加class active
  2. 如果状态为0且类别为客户端,则添加class =&#34; active&#34;和 添加按钮。
  3. 如果状态不是0类别是客户端则添加class =&#34; active1&#34;和 添加按钮
  4. 如果status不为0且category不是client,则只添加 类=&#34;有源&#34;
  5. 因此,如果使用jstl

    循环,请告诉我如何使用if循环

2 个答案:

答案 0 :(得分:1)

而不是多个<c:choose />使用<c:if /><c:var />来确定要使用的类。您还可以使用<:if />作为按钮,这将要求您将图像的渲染移动到css。

JSP看起来像这样(从头到尾)。

<c:forEach items="${parentList}" var="test">
    <c:set var="clazz" value="active" />
    <c:if test="${test[1] ne 0 and test[0].category ne 'client'}">
        <c:set var="clazz" value="active1" />
    </c:if>

    <li>
        <a href="#" class="${clazz}" value="${test[0].id}-${test[0].category}" id="${parent.id}" onclick="return getQuestions(this);" > ${test[0].name }</a>
        <c:if test="${test[0].category eq 'client'}"><button name="delete" /></c:if>
    </li>
</c:forEach>

您的CSS课程active1

需要以下内容
a.active1 {
    background-image: url('/resources/ui_resources/img/checked.jpg');
    background-repeat: no-repeat;
    padding-left: 25px;  /* width of the image plus a little extra padding */
    display: block;
}

答案 1 :(得分:0)

根据您的代码块,如何在c:if阻止后添加<c:choose>代码?

<c:forEach ...>
    <c:choose ...>
    </c:choose>
    <%-- here to check buuton add or not --%>
    <c:if test="${test[0].category == 'client'}"><button name="delete" /></c:if>
</c:forEach