Default value on JSP custom-tag attribute
Solution 1
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>
Solution 2
So I wasn't able to figure out a way to add this to the attribute
directive itself; it appears that the directive does not support this functionality. I was, however, able to create a tag that encapsulates the <c:if>...</c:if>
logic. I had to write the tag in Java since there is no way (that I know of) to use an attribute value as a variable name.
First I wrote the tag file as a Java class:
DefaultTag.java
public class DefaultTag extends BodyTagSupport {
private String var;
private Object value;
//for tag attribute
public void setVar(String var) {
this.var = var;
}
//for tag attribute
public void setValue(Object value) {
this.value = value;
}
public int doEndTag() throws JspException {
Object oldValue = pageContext.getAttribute(var);
Object newValue;
if(value != null) {
newValue = value;
}
else {
if(bodyContent == null || bodyContent.getString() == null) {
newValue = "";
}
else {
newValue = bodyContent.getString().trim();
}
}
if(oldValue == null) {
pageContext.setAttribute(var, newValue);
}
else if(oldValue.toString().trim().length() == 0) {
pageContext.setAttribute(var, newValue);
}
return EVAL_PAGE;
}
}
Then I made a tld
file:
utils.tld:
<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>2.0</tlib-version>
<short-name>utils</short-name>
<uri>http://utils</uri>
<tag>
<name>default</name>
<tag-class>com.mystuff.mvc.tag.DefaultTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
Then I made a custom tag that uses this tag:
defaultTest.tag
<%@ taglib prefix="utils" uri="/WEB-INF/tlds/utils.tld" %>
<%@ attribute name="value" required="true"%>
<%@ attribute name="optValue" required="false"%>
<utils:default var="optValue" value="optional monkeys"/>
${value} ${optValue}
After that I made a page to test the tag I just created:
tagTest.jsp
<mystuff:defaultTest value="helloThar" /><br/><br/>
<mystuff:defaultTest value="helloThere" optValue="monkeys" /><br/><br/>
<mystuff:defaultTest value="helloYou" optValue="${1 + 2 + 4 + 10}" /><br/><br/>
And that gave me:
helloThar optional monkeys
helloThere monkeys
helloYou 17
Vivin Paliath
Check out regula. It is very neat and will make all your dreams come true. Your monkey poured coffee in my boots! I assume that everyone knows more than I do. map{@n=split//;$j.=$n[0]x$n[1]}split/:/,"01:11:02". ":11:01:11:02:13:01:11:01:11:01:13:02:12:01:13:01". ":11:04:11:06:12:04:11:01:12:01:13:02:12:01:14:01". ":13:01:11:03:12:01:11:04:12:02:11:01:11:01:13:02". ":11:03:11:06:11:01:11:05:12:02:11:01:11:01:13:02". ":11:02:12:01:12:04:11:06:12:01:11:04:12:04:11:01". ":12:03:12:01:12:01:11:01:12:01:12:02:11:01:11:01". ":13:02:11:01:02:11:01:12:02";map{print chr unpack" i",pack"B32",$_}$j=~m/.{8}/g
Updated on July 09, 2022Comments
-
Vivin Paliath almost 2 years
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?
-
TheBilTheory over 5 years<c:if test="${empty title ? 'Default Title' : 'New Title'}"> ... </c:if>
-
izogfif over 4 yearsOK, what about the case when attribute is specified and an empty string is passed (and it's supposed to use this empty string instead of default value)? How to distinguish between cases "no attribute is specified", "attribute is specified but an empty value is passed", "attribute is specified but a null value is passed", "attribute is not specified, but there is a non-empty servlet variable with the same name"?