Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...