1

I have the following piece of code (jsp)

        ...
        <hr>
        <div class="actions">
            <div class="btn-toolbar">
                <c:if test="${condition1}">
                    <button type="button" class="btn" >
                        btn1
                    </button>
                </c:if>
                <c:if test="${condition2}">
                    <button type="button" class="btn">
                        btn2
                    </button>
                </c:if>
                <c:if test="${condition3}">
                    <button type="button" class="btn">
                        btn3
                    </button>
                </c:if>
            </div>
        </div>
        <hr> 
        ...

Depending on the conditions, div may be empty, and then we get a double hr-tag. Is there an elegant way to display first hr-tag only if div is not empty?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Natsuo
  • 63
  • 7

3 Answers3

3

Save c:if test results to variables?

<div class="actions">
    <div class="btn-toolbar">
        <c:if test="${condition1}" var="rt1">
            <button type="button" class="btn" >
                btn1
            </button>
        </c:if>
        <c:if test="${condition2}" var="rt2">
            <button type="button" class="btn">
                btn2
            </button>
        </c:if>
        <c:if test="${condition3}" var="rt3">
            <button type="button" class="btn">
                btn3
            </button>
        </c:if>
    </div>
</div>
<c:if test="${rt1 || rt2 || rt3}">
<hr> 
</c:if>
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
emptyhua
  • 6,634
  • 10
  • 11
1

use ::empty in css instead


as blow:
div.action{
  border-top:1px solid #cdcdcd;
  border-bottom:1px solid #cdcdcd
}
  div.action::empty{
  border-top:1px solid #cdcdcd
}
Ramin6032
  • 9
  • 1
0

I'm not too familiar with JSP but you could try wrapping one of the hr in a separate if condition like this:

<c:if test="${condition1 || condition2 || condition3}">
  <hr>
</c:if>
AndrewL64
  • 15,794
  • 8
  • 47
  • 79