Conditionally set an attribute on an element with JSP Documents (JSPX)

I use a custom JSP tag with dynamic attributes. You use it like this:

<util:element elementName="button" name="btn1" disabled="$(isDisabled ? 'disabled' : '')"/>

Basically, what this tag does is generate an XML element with elementName and puts all attributes present in the tag, but skips the empty ones.

The tag itself is pretty easy to implement, my implementation is just 44 lines long.


@alex great solution to use the ternary operator. I add some of my example, that thanks to you, I just changed the result of the condition, if true, writes the attribute, otherwise not write anything

to populate the list and select the value used, avoiding c:if

<select id="selectLang" name="selectLang" >
<c:forEach var="language" items="${alLanguages}" >
    <option value="${language.id}" ${language.code == usedLanguage ? 'selected' : ''} >${language.description}</option>
</c:forEach>

to check at start a radio button to avoiding c:if:

<input type="radio" id="id0" value="0" name="radio" ${modelVar == 0 ? 'checked' : ''} />
<input type="radio" id="id1" value="1" name="radio" ${modelVar == 1 ? 'checked' : ''} />
<input type="radio" id="id2" value="2" name="radio" ${modelVar == 2 ? 'checked' : ''} />