55

is it possible to do something like this in JSTL:

<div class="firstclass<c:if test='true'> someclass</c:if>">
   <p>some other stuff...</p>
</div>

Is there any way to get this to work, or is there a better way to add a class by looking at a JSTL-if-statement?

Mickel
  • 6,658
  • 5
  • 42
  • 59

4 Answers4

144

It's also possible to use an EL expression directly like this:

<div class="${booleanExpr ? 'cssClass' : 'otherCssClass'}">
</div>
runfa
  • 1,984
  • 2
  • 13
  • 12
42

<c:if test='true'> 
  <c:set value="someclass" var="cssClass"></c:set>
</c:if> 
<div class="${cssClass}">
   <p>some other stuff...</p>
</div>
simon
  • 12,666
  • 26
  • 78
  • 113
-1

For me the following worked in combination with Java:

<%
String cssClass = 'oneClass';

if(i >= 5){
   cssClass += " anotherClass";
}

<div cssClass="<%= cssClass %>" >... </div>
%>


Michelle
  • 69
  • 1
  • 8
-3

That works for me!

<div id="loginById" style="height:<% out.print(instanceClass.booleanMethod()? "250px": "150px");%>">
elciospy
  • 260
  • 4
  • 11