From 968014b4d739dca08b9536f6b0877e7bda7ed271 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 11 May 2010 15:38:28 +0000 Subject: [PATCH] SWF-1238 Ensure the response is committed when rendering multiple fragments. --- .../publish-documentation.xml | 2 +- .../js/ajax/tiles2/AjaxTilesView.java | 9 +++--- .../js/ajax/tiles2/AjaxTilesViewTests.java | 32 +++++++++++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/build-spring-webflow/publish-documentation.xml b/build-spring-webflow/publish-documentation.xml index ed400564..094f8cb9 100644 --- a/build-spring-webflow/publish-documentation.xml +++ b/build-spring-webflow/publish-documentation.xml @@ -17,7 +17,7 @@ + command="rm /opt/www/domains/springframework.org/www/htdocs/spring-webflow/docs/2.1.x ; ln -s /opt/www/domains/springframework.org/www/htdocs/spring-webflow/docs/${bundle.version} /opt/www/domains/springframework.org/www/htdocs/spring-webflow/docs/2.1.x ; ln -s /opt/www/domains/springframework.org/www/htdocs/spring-webflow/docs/${bundle.version}/spring-webflow-reference/ /opt/www/domains/springframework.org/www/htdocs/spring-webflow/docs/${bundle.version}/reference" /> diff --git a/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java b/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java index a4688e32..693171af 100644 --- a/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java +++ b/spring-js/src/main/java/org/springframework/js/ajax/tiles2/AjaxTilesView.java @@ -111,10 +111,11 @@ public class AjaxTilesView extends TilesView { response); addRuntimeAttributes(container, flattenedAttributeMap, request, response); - // initialize the session before rendering any fragments. Otherwise views that require the session which has - // not otherwise been initialized will fail to render - // request.getSession(); - // response.flushBuffer(); + if (fragmentsToRender.length > 1) { + // When rendering more than one fragment, flush the buffer in order to commit the response. + // With the response committed, Tiles does an include rather than forward. + response.flushBuffer(); + } for (int i = 0; i < fragmentsToRender.length; i++) { Attribute attributeToRender = (Attribute) flattenedAttributeMap.get(fragmentsToRender[i]); diff --git a/spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesViewTests.java b/spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesViewTests.java index 98a24d3e..19789c60 100644 --- a/spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesViewTests.java +++ b/spring-js/src/test/java/org/springframework/js/ajax/tiles2/AjaxTilesViewTests.java @@ -1,6 +1,8 @@ package org.springframework.js.ajax.tiles2; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import junit.framework.TestCase; @@ -26,14 +28,14 @@ public class AjaxTilesViewTests extends TestCase { private AjaxTilesView ajaxTilesView; private MockHttpServletRequest request; - private MockHttpServletResponse response; + private MultipleIncludeUrlMockHttpServletResponse response; private MockServletContext servletContext; protected void setUp() throws Exception { servletContext = new MockServletContext("/org/springframework/js/ajax/tiles2/"); request = new MockHttpServletRequest(servletContext); - response = new MockHttpServletResponse(); + response = new MultipleIncludeUrlMockHttpServletResponse(); TilesConfigurer tc = new TilesConfigurer(); tc.setDefinitions(new String[] { "tiles-definitions.xml" }); @@ -112,6 +114,18 @@ public class AjaxTilesViewTests extends TestCase { container.endContext(requestItems); } + public void testRenderFragment_Multiple() throws Exception { + setupStaticWebApplicationContext(); + request.addHeader("Accept", SpringJavascriptAjaxHandler.AJAX_ACCEPT_CONTENT_TYPE); + request.addParameter("fragments", "body,searchNavigation"); + ajaxTilesView.setUrl("search"); + ajaxTilesView.afterPropertiesSet(); + ajaxTilesView.renderMergedOutputModel(new HashMap(), request, response); + assertTrue("Multiple fragments should result in include, not forward", response.getIncludedUrls().size() == 2); + assertEquals("/WEB-INF/search.jsp", response.getIncludedUrls().get(0)); + assertEquals("/WEB-INF/searchNavigation.jsp", response.getIncludedUrls().get(1)); + } + public void testFlattenAttributeMap() throws Exception { TilesRequestContextFactory tilesRequestContextFactory = new ServletTilesRequestContextFactory(); tilesRequestContextFactory.init(new HashMap()); @@ -136,4 +150,18 @@ public class AjaxTilesViewTests extends TestCase { assertEquals("f2", fragments[1]); assertEquals("f3", fragments[2]); } + + // Remove this class when no longer needed (http://jira.springframework.org/browse/SPR-7188) + private class MultipleIncludeUrlMockHttpServletResponse extends MockHttpServletResponse { + List includedUrls = new ArrayList(); + + public void setIncludedUrl(String includedUrl) { + super.setIncludedUrl(includedUrl); + includedUrls.add(includedUrl); + } + + public List getIncludedUrls() { + return includedUrls; + } + } }