如何在使用jstl的if循环中使用if循环 [英] how to use if loop inside if loop using jstl

查看:76
本文介绍了如何在使用jstl的if循环中使用if循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,如果状态为0,则添加一个css类为active,否则添加一个css类为active1,为此,我按照以下步骤进行操作

I have a requirement where if status is 0 then add a css class active else add a css class active1 and for that I have done like the follow

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

${test[0].category} is client then add a button <button name="delete/>

所以现在出现了4个条件

so now 4 condtions arrise

  1. 如果状态为0且类别不是客户端,则添加活动类别
  2. 如果状态为0且类别为客户端,则添加类="active"并 添加按钮.
  3. 如果状态不是0,则类别为client,则添加class ="active1",然后 添加按钮
  4. 如果状态不为0并且类别不是客户端,则仅添加 class ="active".
  1. if status is 0 and category is not client then add class active
  2. if status is 0 and category is client then add class ="active" and add button.
  3. if status is not 0 category is client then add class="active1" and add button
  4. if status is not 0 and category is not client then only add class="active".

所以任何机构都可以告诉我如何在使用jstl的if循环内部使用if循环

So can any body please tell me how to use if loop inside if loop using jstl

推荐答案

使用<c:if />而不是<c:var />代替<c:choose />来确定要使用的类.您还可以对按钮使用<:if />,这将需要将图像的渲染移至CSS.

Instead of a multitude of <c:choose /> use <c:if /> together with a <c:var /> to determine the class to use. You can also use a <:if /> for the button, this will require you to move the rendering of the image to the css.

JSP看起来像这样(从我的头顶开始).

The JSP would look something like this (from the top of my head).

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

这篇关于如何在使用jstl的if循环中使用if循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆