You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ma...@apache.org on 2004/01/27 02:42:32 UTC
cvs commit: jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts TilesTool.java
marino 2004/01/26 17:42:32
Modified: src/java/org/apache/velocity/tools/struts TilesTool.java
Log:
Added three new methods - two to import named attributes from the current tiles definition to either the current velocity context, or a named context ("page", "request", "session", or "application") - and one method to add all attributes of the current tiles definition to a named context ("page", "request", "session", or "application").
Revision Changes Path
1.9 +118 -13 jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/TilesTool.java
Index: TilesTool.java
===================================================================
RCS file: /home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/struts/TilesTool.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- TilesTool.java 2 Dec 2003 19:20:14 -0000 1.8
+++ TilesTool.java 27 Jan 2004 01:42:32 -0000 1.9
@@ -105,6 +105,10 @@
public class TilesTool extends ImportSupport
implements ViewTool
{
+ static final String PAGE_SCOPE = "page";
+ static final String REQUEST_SCOPE = "request";
+ static final String SESSION_SCOPE = "session";
+ static final String APPLICATION_SCOPE = "application";
protected ViewContext context;
@@ -151,31 +155,75 @@
*/
public void importAttributes()
{
- ComponentContext currentContext =
- ComponentContext.getContext(this.request);
+ this.importAttributes(PAGE_SCOPE);
+ }
+
+ /**
+ * Imports all attributes in the current tiles-context into the named
+ * context ("page", "request", "session", or "application").
+ *
+ * <p>This is functionally equivalent to
+ * <code><tiles:importAttribute scope="scopeValue" /></code>.</p>
+ *
+ * @param scope the named context scope to put the attributes into.
+ */
+ public void importAttributes(String scope)
+ {
+ ComponentContext currentContext = ComponentContext.getContext(this.
+ request);
Iterator names = currentContext.getAttributeNames();
- Context velocityContext = this.context.getVelocityContext();
- while (names.hasNext())
+
+ if (scope.equals(PAGE_SCOPE))
+ {
+ while (names.hasNext())
+ {
+ String name = (String)names.next();
+ context.getVelocityContext().put(name,
+ currentContext.
+ getAttribute(name));
+ }
+ }
+ else if (scope.equals(REQUEST_SCOPE))
{
- String name = (String)names.next();
- velocityContext.put(name, currentContext.getAttribute(name));
+ while (names.hasNext())
+ {
+ String name = (String)names.next();
+ request.setAttribute(name, currentContext.getAttribute(name));
+ }
+ }
+ else if (scope.equals(SESSION_SCOPE))
+ {
+ while (names.hasNext())
+ {
+ String name = (String)names.next();
+ request.getSession().setAttribute(name,
+ currentContext.
+ getAttribute(name));
+ }
+ }
+ else if (scope.equals(APPLICATION_SCOPE))
+ {
+ while (names.hasNext())
+ {
+ String name = (String)names.next();
+ application.setAttribute(name, currentContext.getAttribute(name));
+ }
}
}
+
/**
* Fetches a named attribute-value from the current tiles-context.
*
* <p>This is functionally equivalent to
- * <code><tiles:importAttribute name="attributeName" /></code>
- * as well as
* <code><tiles:getAsString name="attributeName" /></code>.</p>
*
- * @param attributeName the name of the tiles-attribute to import
+ * @param attributeName the name of the tiles-attribute to fetch
* @return attribute value for the named attribute
*/
public Object getAttribute(String attributeName)
{
- ComponentContext currentContext =
+ ComponentContext currentContext =
ComponentContext.getContext(this.request);
Object value = currentContext.getAttribute(attributeName);
if (value == null)
@@ -188,6 +236,63 @@
}
/**
+ * Imports the named attribute-value from the current tiles-context into the
+ * current Velocity context.
+ *
+ * <p>This is functionally equivalent to
+ * <code><tiles:importAttribute name="attributeName" /></code>
+ *
+ * @param attributeName the name of the tiles-attribute to import
+ */
+ public void importAttribute(String attributeName)
+ {
+ this.importAttribute(attributeName, PAGE_SCOPE);
+ }
+
+ /**
+ * Imports the named attribute-value from the current tiles-context into the
+ * named context ("page", "request", "session", or "application").
+ *
+ * <p>This is functionally equivalent to
+ * <code><tiles:importAttribute name="attributeName" scope="scopeValue" /></code>
+ *
+ * @param attributeName the name of the tiles-attribute to import
+ * @param scope the named context scope to put the attribute into.
+ */
+ public void importAttribute(String attributeName, String scope)
+ {
+ ComponentContext currentContext =
+ ComponentContext.getContext(this.request);
+ Object value = currentContext.getAttribute(attributeName);
+ if (value == null)
+ {
+ Velocity.warn("TilesTool: Tile attribute '"
+ + attributeName
+ + "' was not found in context.");
+ }
+
+ if (scope.equals(PAGE_SCOPE))
+ {
+ context.getVelocityContext().put(attributeName, value);
+ }
+ else if (scope.equals(REQUEST_SCOPE))
+ {
+ request.setAttribute(attributeName, value);
+ }
+ else if (scope.equals(SESSION_SCOPE))
+ {
+ request.getSession().setAttribute(attributeName, value);
+ }
+ else if (scope.equals(APPLICATION_SCOPE))
+ {
+ application.setAttribute(attributeName, value);
+ }
+
+ }
+
+
+
+ /**
* <p>A generic tiles insert function</p>
*
* <p>This is functionally equivalent to
@@ -206,7 +311,7 @@
{
try
{
- ComponentContext currentContext =
+ ComponentContext currentContext =
ComponentContext.getContext(this.request);
Object attrValue = currentContext.getAttribute(attr.toString());
if (attrValue != null)
@@ -217,7 +322,7 @@
}
catch (Exception e)
{
- Velocity.error("TilesTool: Exeption while rendering Tile "
+ Velocity.error("TilesTool: Exeption while rendering Tile "
+ attr + ": " + e.getMessage());
return null;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org