Is there a way to conditionally include attributes in custom JSP tag?

158 Views Asked by At

Lets say I have a field that can take 3 attributes

<myTag a="something" b="something" c="something" />

Let's say I want to only include those a,b,c attributes when a variables are not blank. So with EL it becomes something like this

<myTag a="${varA}" b="${varB}" c="${varC}" />

In the case that one or more of the variables are blank (say varB is empty) I'm getting output like:

<myTag a="a val" b c="c val" />

So b is still there, but passing a blank value. This can cause problems with some spring <form:etc> tags if an attribute is blank (say itemLabel on form:options for example) What if I don't want that? Is there an easy way to have the attribute not show up altogether?

I get that I can do

<c:if test="${empty b}">
    <myTag a="${varA}" c="${varC}" />
</c:if>

But that means we need a giant decision tree as the number of attributes increase if each one can possibly be blank.

Does anyone know a better way to do this?

0

There are 0 best solutions below