'Conditional JSP tag-attribute
Is there a way to conditionally omit a JSP tag attribute? I have a template that is called multiple times and the tag has to be set conditionally.
I tried to pass null:
<sometaglib:sometag someattrib="${empty myvar ? null : myvar}">
...
</sometaglib:sometag>
Unfortunately the tag has a setter method for someattrib which validates the value. A null value is not allowed. Only if I completely omit the attribute, the setter won't be called.
A solution would be to add all combinations and wrap them with <c:if>:
<c:if test="${not empty myvar}">
<sometaglib:sometag someattrib="${empty myvar ? null : myvar}">
...
</sometaglib:sometag>
</c:if>
<c:if test="${empty myvar}">
<sometaglib:sometag>
...
</sometaglib:sometag>
</c:if>
The problem is that the tag has content and other attributes. And all that content has to be duplicated. And if you have more attributes like that you'll end up with 2^n combinations.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
