JSTL if-statement inside HTML-attribute

CssJspJstlJsp Tags

Css Problem Overview


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?

Css Solutions


Solution 1 - Css

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

<div class="${booleanExpr ? 'cssClass' : 'otherCssClass'}">
</div>

Solution 2 - Css


<c:if test='true'>
<c:set value="someclass" var="cssClass"></c:set>
</c:if>
<div class="${cssClass}">
<p>some other stuff...</p>
</div>

Solution 3 - Css

That works for me!

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMickelView Question on Stackoverflow
Solution 1 - CssrunfaView Answer on Stackoverflow
Solution 2 - CsssimonView Answer on Stackoverflow
Solution 3 - CsselciospyView Answer on Stackoverflow