Add Spring Framework integration with Tiles 3
Tiles 3 has modified packages and classes as well as additional
dependencies, notably "tiles-request-api", which is a request/response
abstraction independent of Servlet and JSP APIs.
In order to have both Tiles 2 and Tiles 3 integrations, the source for
the Tiles 3 integration is in a separate project spring-webmvc-tiles3.
The build process merges the compiled Tiles 3 integration classes into
the spring-webmvc module so in effect it contains both the Tiles 2 and
the Tiles 3 integrations.
This change originated as a pull request at spring-framework-issues:
https://github.com/SpringSource/spring-framework-issues/pull/30
And was additionally updated:
1f64be4aa5
Issue: SPR-8825
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.view.tiles3;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.apache.tiles.access.TilesAccess;
|
||||
import org.apache.tiles.impl.BasicTilesContainer;
|
||||
import org.apache.tiles.request.ApplicationContext;
|
||||
import org.apache.tiles.request.Request;
|
||||
import org.apache.tiles.request.servlet.ServletRequest;
|
||||
import org.apache.tiles.request.servlet.ServletUtil;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link TilesConfigurer}.
|
||||
*
|
||||
* @author Nicolas Le Bas
|
||||
*/
|
||||
public class TilesConfigurerTests {
|
||||
|
||||
@Test
|
||||
public void simpleBootstrap() {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
|
||||
TilesConfigurer tc = new TilesConfigurer();
|
||||
tc.setDefinitions(new String[] { "/org/springframework/web/servlet/view/tiles3/tiles-definitions.xml" });
|
||||
tc.setCheckRefresh(true);
|
||||
tc.setServletContext(servletContext);
|
||||
tc.afterPropertiesSet();
|
||||
|
||||
ApplicationContext tilesContext = ServletUtil.getApplicationContext(servletContext);
|
||||
|
||||
BasicTilesContainer container = (BasicTilesContainer) TilesAccess.getContainer(tilesContext);
|
||||
Request requestContext = new ServletRequest(container.getApplicationContext(),
|
||||
new MockHttpServletRequest(), new MockHttpServletResponse());
|
||||
assertNotNull(container.getDefinitionsFactory().getDefinition("test", requestContext));
|
||||
|
||||
tc.destroy();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class AppConfig {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.web.servlet.view.tiles3;
|
||||
|
||||
import static org.easymock.EasyMock.eq;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.isA;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.tiles.request.Request;
|
||||
import org.apache.tiles.request.render.Renderer;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link TilesViewResolver}.
|
||||
*
|
||||
* @author mick semb wever
|
||||
*/
|
||||
public class TilesViewResolverTests {
|
||||
|
||||
private TilesViewResolver viewResolver;
|
||||
|
||||
private Renderer renderer;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(new MockServletContext());
|
||||
wac.refresh();
|
||||
|
||||
this.renderer = EasyMock.createMock(Renderer.class);
|
||||
|
||||
this.viewResolver = new TilesViewResolver();
|
||||
this.viewResolver.setRenderer(this.renderer);
|
||||
this.viewResolver.setApplicationContext(wac);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolve() throws Exception {
|
||||
expect(this.renderer.isRenderable(eq("/template.test"), isA(Request.class))).andReturn(true);
|
||||
expect(this.renderer.isRenderable(eq("/nonexistent.test"), isA(Request.class))).andReturn(false);
|
||||
replay(this.renderer);
|
||||
|
||||
assertTrue(this.viewResolver.resolveViewName("/template.test", Locale.ITALY) instanceof TilesView);
|
||||
assertNull(this.viewResolver.resolveViewName("/nonexistent.test", Locale.ITALY));
|
||||
|
||||
verify(this.renderer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.web.servlet.view.tiles3;
|
||||
|
||||
import static org.easymock.EasyMock.and;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.eq;
|
||||
import static org.easymock.EasyMock.isA;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.tiles.request.ApplicationContext;
|
||||
import org.apache.tiles.request.Request;
|
||||
import org.apache.tiles.request.render.Renderer;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link TilesView}.
|
||||
*
|
||||
* @author mick semb wever
|
||||
*/
|
||||
public class TilesViewTests {
|
||||
|
||||
private static final String VIEW_PATH = "template.test";
|
||||
|
||||
private TilesView view;
|
||||
|
||||
private Renderer renderer;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(servletContext);
|
||||
wac.refresh();
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
response = new MockHttpServletResponse();
|
||||
|
||||
renderer = createMock(Renderer.class);
|
||||
|
||||
view = new TilesView();
|
||||
view.setServletContext(servletContext);
|
||||
view.setRenderer(renderer);
|
||||
view.setUrl(VIEW_PATH);
|
||||
view.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRender() throws Exception {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("modelAttribute", "modelValue");
|
||||
|
||||
ApplicationContext tilesContext = createMock(ApplicationContext.class);
|
||||
|
||||
renderer.render(eq(VIEW_PATH), and(isA(Request.class), isA(Request.class)));
|
||||
replay(tilesContext, renderer);
|
||||
|
||||
view.render(model, request, response);
|
||||
|
||||
assertEquals("modelValue", request.getAttribute("modelAttribute"));
|
||||
verify(tilesContext, renderer);
|
||||
}
|
||||
|
||||
}
|
||||
7
spring-webmvc-tiles3/src/test/resources/log4j.properties
Normal file
7
spring-webmvc-tiles3/src/test/resources/log4j.properties
Normal file
@@ -0,0 +1,7 @@
|
||||
log4j.rootCategory=INFO, stdout
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
|
||||
log4j.category.org.springframework.web=DEBUG
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE tiles-definitions PUBLIC
|
||||
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
|
||||
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
|
||||
|
||||
<tiles-definitions>
|
||||
|
||||
<definition name="test" template="/WEB-INF/tiles/test.jsp"/>
|
||||
|
||||
</tiles-definitions>
|
||||
Reference in New Issue
Block a user