Optional return value from JSP custom tag?
Hi,
I'm writing JSP custom tags, and I want to be able to optionally provide a variable name for a return value (like several core tags, including :url and :import).
It works fine if I require the return value variable, but I cannot seem to make it optional.
Here is the relevant part of working tag file mytag.tag:
<%@ tag body-content="scriptless" %>
<%@ attribute name="cond" required="true" %>
<%@ attribute name="var" required="true" rtexprvalue="false" %>
<%@ variable alias="ret" name-from-attribute="var" scope="AT_BEGIN" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:set value="i am the return value" var="ret" />
When the above code is called like this:
<%@ taglib prefix="lmtags" tagdir="/WEB-INF/tags" %>
<lmtags:mytag cond="sunny" var="test" />
The value "i am the return value" is stored in a page-scoped variable called "test" in the calling JSP.
However, if I change the 'required' attribute from true to false on the 'var' attribute declaration, I get the following exception:
java.lang.RuntimeException: org.apache.jasper.JasperException: /WEB-INF/tags/mytag.tag(4,1) The attribute directive (declared in line 3 and whose name attribute is"var", the value ofthis name-from-attribute attribute) must be of type java.lang.String, is"required" and not a"rtexprvalue".
at org.apache.jasper.compiler.ImplicitTagLibraryInfo.getTagFile(ImplicitTagLibraryInfo.java:129)
at org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1315)
at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1577)
at org.apache.jasper.compiler.Parser.parseBody(Parser.java:1806)
(This is with Tomcat 5.5, jstl-1.1.2.jar, and standard-1.1.2.jar).
Is there a way to make the return value variable optional, so that a value is either returned as the name-from-attribute variable if passed, or output directly if not?
Thanks.

