Search code examples
javajspjsp-tagsdefault-value

Default value on JSP custom-tag attribute


When defining an attribute for a custom JSP tag, is it possible to specify a default value? The attribute directive doesn't have a default value attribute. Currently I'm making do with:

<%@ attribute name="myAttr" required="false" type="java.lang.String" %>

<c:if test="${empty myAttr}" >
 <c:set var="myAttr" value="defaultValue" />
</c:if>

Is there a better way?


Solution

  • There is a better way:

    <c:set var="title" value="${(empty title) ? 'Default title' : title}" />
    

    No need for custom tag in Java nor tld. Just plain JSP EL and conditional operator.


    In my opinion it is shorter and cleaner than old:

    <c:if test="${empty title}" >
     <c:set var="title" value="Default title" />
    </c:if>