Return-Path: Delivered-To: apmail-click-commits-archive@www.apache.org Received: (qmail 9866 invoked from network); 2 Jun 2010 11:40:07 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 2 Jun 2010 11:40:07 -0000 Received: (qmail 60844 invoked by uid 500); 2 Jun 2010 11:40:06 -0000 Delivered-To: apmail-click-commits-archive@click.apache.org Received: (qmail 60829 invoked by uid 500); 2 Jun 2010 11:40:06 -0000 Mailing-List: contact commits-help@click.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: click-dev@click.apache.org Delivered-To: mailing list commits@click.apache.org Received: (qmail 60821 invoked by uid 99); 2 Jun 2010 11:40:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Jun 2010 11:40:05 +0000 X-ASF-Spam-Status: No, hits=-1777.3 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Jun 2010 11:40:04 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 9DBBE23889DD; Wed, 2 Jun 2010 11:39:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r950501 - /click/trunk/click/framework/src/org/apache/click/Partial.java Date: Wed, 02 Jun 2010 11:39:44 -0000 To: commits@click.apache.org From: sabob@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100602113944.9DBBE23889DD@eris.apache.org> Author: sabob Date: Wed Jun 2 11:39:44 2010 New Revision: 950501 URL: http://svn.apache.org/viewvc?rev=950501&view=rev Log: added template support to Partials Modified: click/trunk/click/framework/src/org/apache/click/Partial.java Modified: click/trunk/click/framework/src/org/apache/click/Partial.java URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/Partial.java?rev=950501&r1=950500&r2=950501&view=diff ============================================================================== --- click/trunk/click/framework/src/org/apache/click/Partial.java (original) +++ click/trunk/click/framework/src/org/apache/click/Partial.java Wed Jun 2 11:39:44 2010 @@ -20,21 +20,22 @@ package org.apache.click; import java.io.InputStream; import java.io.OutputStream; -import java.io.PrintWriter; import java.io.Reader; import java.io.StringReader; +import java.io.Writer; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.click.util.ClickUtils; /** * TODO rename partial as its used for both PageAction and Ajax? * + * TODO add support for rendering a given template + * * Partial encapsulates a fragment of an HTTP response. A Partial can be used * to stream back a String or byte array to the browser. * @@ -102,9 +103,52 @@ public class Partial { /** Indicates whether the Partial should be cached by browser. */ private boolean cachePartial = false; + /** The path of the partial template to render. */ + private String template; + + /** The model for the Partial {@link #template}. */ + private Map model; + // ----------------------------------------------------------- Constructors /** + * Construct the Partial for the given template and model. + *

+ * When the Partial is rendered the template and model will be merged and + * the result will be streamed back to the client. + *

+ * For example: + *

+     * public class MyPage extends Page {
+     *     public void onInit() {
+     *
+     *         Behavior behavior = new DefaultAjaxBehavior() {
+     *
+     *             public Partial onAction() {
+     *
+     *                 Map model = new HashMap();
+     *                 model.put("id", "link");
+     *
+     *                 // Note: we set XML as the content type
+     *                 Partial partial = new Partial("/js/partial.xml", model, Partial.XML);
+     *
+     *                 return partial;
+     *             }
+     *         }
+     *     }
+     * } 
+ * + * @param template the template to render and stream back to the client + * @param model the template data model + * @param contentType the response content type + */ + public Partial(String template, Map model, String contentType) { + this.template = template; + this.model = model; + this.contentType = contentType; + } + + /** * Construct the Partial for the given reader and content type. * * @param reader the reader which characters must be streamed back to the @@ -337,30 +381,82 @@ public class Partial { } /** + * Return the data model for the Partial {@link #template}. + * + * @return the data model for the Partial template + */ + public Map getModel() { + if (model == null) { + model = new HashMap(); + } + return model; + } + + /** + * Set the model of the Partial template to render. + *

+ * If the {@link #template} property is set, the template and {@link #model} + * will be merged and the result will be streamed back to the client. + * + * @param model the model of the template to render + */ + public void setModel(Map model) { + this.model = model; + } + + /** + * Return the template to render for this partial. + * + * @return the template to render for this partial + */ + public String getTemplate() { + return template; + } + + /** + * Set the template to render for this partial. + * + * @param template the template to render for this partial + */ + public void setTemplate(String template) { + this.template = template; + } + + /** * Process the partial with the given context. * - * @param context the request context to use + * @param context the request context */ public final void render(Context context) { prepare(context); - render(context.getRequest(), context.getResponse()); + renderPartial(context); } /** - * Render the partial to the specified response. + * Render the partial response to the client. * - * @param request the page servlet request - * @param response the page servlet response + * @param context the request context */ - protected void render(HttpServletRequest request, HttpServletResponse response) { + protected void renderPartial(Context context) { + + HttpServletResponse response = context.getResponse(); try { - if (content != null) { + String template = getTemplate(); + if (template != null) { + Map templateModel = getModel(); + if (templateModel == null) { + templateModel = new HashMap(); + } + String result = context.renderTemplate(template, templateModel); + this.reader = new StringReader(result); + + } else if (content != null) { this.reader = new StringReader(content.toString()); } if (reader != null) { - PrintWriter writer = response.getWriter(); + Writer writer = response.getWriter(); char[] buffer = new char[WRITER_BUFFER_SIZE]; int len = 0; while (-1 != (len = reader.read(buffer))) { @@ -458,4 +554,4 @@ public class Partial { response.setContentType(getContentType() + "; charset=" + getCharacterEncoding()); } } -} +}