I added the ability to specifiy default content for the template:get tag.
This content is output by the get tag only if there is no corresponding
template:put tag in the inserting page. I did this by making it so
template:get accepts a "content" attribute and "direct" attribute, and can
also have a body. These have the same meaning as for the template:put tag,
except that they are only evaluated if there is no content with the given
name on the ContentMapStack. Here is an example of the usage:
<!-- template.jsp -->
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<template:get name="title" content="Default Title" direct="true"/>
<template:get name="body">
This is the default page body
</template:get>
<!-- page.jsp -->
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<template:insert template="template.jsp">
<template:put name="title" content="Page Title" direct="true"/>
<!-- I don't specify a body, so default will be used. -->
</template:insert>
In the above example, page.jsp inserts template.jsp, and only specifiies a
title. So in template.jsp, <template:get name="title"> will output the title
given in page.jsp, but <template:get name="body"> will use the default body
specified in template.jsp.
My motivation for adding this is that often when I write templates, I want to
make them very configurable. But often there are sensible defaults for most
of the parameters. It makes the templates much more useful if when I insert
them, I only need to specify values where I want to override the defaults.
I had to modify two files to add this:
src/share/org/apache/struts/taglib/template/GetTag.java
doc/userGuide/struts-template.xml
Here are the patches.
Index: struts-template.xml
===================================================================
RCS file: /home/cvspublic/jakarta-struts/doc/userGuide/struts-template.xml,v
retrieving revision 1.1
diff -u -r1.1 struts-template.xml
--- struts-template.xml 20 Feb 2002 00:41:14 -0000 1.1
+++ struts-template.xml 6 Apr 2002 16:51:01 -0000
@@ -117,7 +117,7 @@
put tag.
</summary>
<tagclass>org.apache.struts.taglib.template.GetTag</tagclass>
- <bodycontent>empty</bodycontent>
+ <bodycontent>JSP</bodycontent>
<info>
Retrieve content from request scope and include it.
</info>
@@ -154,6 +154,26 @@
</info>
</attribute>
+ <attribute>
+ <name>content</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ <info>
+ Content that's put into request scope if there is no
+ corresponding put element.
+ </info>
+ </attribute>
+
+ <attribute>
+ <name>direct</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ <info>
+ Determines how content is handled: true means content is
+ printed <i>direct</i>ly; false, the default, means content
+ is included.
+ </info>
+ </attribute>
</tag>
Index: GetTag.java
===================================================================
RCS file:
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/taglib/template/GetTag.java,v
retrieving revision 1.12
diff -u -r1.12 GetTag.java
--- GetTag.java 12 Mar 2002 05:55:08 -0000 1.12
+++ GetTag.java 6 Apr 2002 16:52:19 -0000
@@ -67,6 +67,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.action.Action;
import org.apache.struts.taglib.template.util.*;
@@ -79,7 +80,7 @@
* @author David Geary
* @version $Revision: 1.12 $ $Date: 2002/03/12 05:55:08 $
*/
-public class GetTag extends TagSupport {
+public class GetTag extends BodyTagSupport {
// ----------------------------------------------------- Instance Variables
@@ -101,6 +102,18 @@
private String role;
/**
+ * The default content's URI (or text).
+ */
+ private String content = null;
+
+
+ /**
+ * Determines whether content is included (false) or printed (true).
+ * Content is included (false) by default.
+ */
+ private String direct = null;
+
+ /**
* Set the flush-before-include property
* @param flush The new flush property
*/
@@ -131,6 +144,27 @@
}
/**
+ * Set the content's URI (if it's to be included) or text (if it's to
+ * be printed).
+ */
+ public void setContent(String content) {
+
+ this.content = content;
+
+ }
+
+
+ /**
+ * Set direct to true, and content will be printed directly, instead
+ * of included (direct == false).
+ */
+ public void setDirect(String direct) {
+
+ this.direct = direct;
+
+ }
+
+ /**
* Get the flush-before-include attribute.
*/
public boolean getFlush() {
@@ -158,6 +192,24 @@
}
+
+ /**
+ * Get the content attribute.
+ */
+ public String getContent() {
+
+ return content;
+
+ }
+
+ /**
+ * Returns the direct attribute associated with this tag.
+ */
+ public String getDirect() {
+ if(hasBody()) return "true";
+ else return direct == null ? "false" : "true";
+ }
+
// --------------------------------------------------------- Public Methods
/**
@@ -174,6 +226,29 @@
ContentMap map = ContentMapStack.peek(pageContext);
Content content = map.get(name);
+ if (content == null) {
+ // see if default content was provided
+ String bodyAndContentMismatchError =
+ "Please specify template content in this tag's body " +
+ "or with the content attribute, but not both.",
+ bodyAndDirectMismatchError =
+ "If content is specified in the tag body, the " +
+ "direct attribute must be true.";
+
+ boolean hasBody = hasBody(), contentSpecified = (this.content != null);
+
+ if(hasBody && contentSpecified)
+ throw new JspException(bodyAndContentMismatchError);
+
+ if(hasBody && direct != null && direct.equalsIgnoreCase("false"))
+ throw new JspException(bodyAndDirectMismatchError);
+
+ String actualContent =
+ hasBody ? bodyContent.getString() : this.content;
+ if (actualContent != null)
+ content = new Content(actualContent, direct);
+ }
+
if(content != null) {
if(content.isDirect()) {
try {
@@ -202,7 +277,16 @@
}
+ /**
+ * Returns a boolean indicating whether this tag has a body.
+ */
+ private boolean hasBody() {
+ if (bodyContent == null)
+ return (false);
+ return ! bodyContent.getString().equals("");
+ }
+
/**
* Reset member values for reuse. This method calls super.release(),
* which invokes TagSupport.release(), which typically does nothing.
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>