Skip to content

Commit cfa3d35

Browse files
committed
Add an alwaysInclude property to TilesViewResolver
Issue: SPR-12374
1 parent 01382b8 commit cfa3d35

File tree

5 files changed

+105
-15
lines changed

5 files changed

+105
-15
lines changed

spring-webmvc-tiles2/src/main/java/org/springframework/web/servlet/view/tiles2/TilesView.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,12 +48,27 @@
4848
* {@link TilesConfigurer} bean definition in the application context.
4949
*
5050
* @author Juergen Hoeller
51+
* @author Sebastien Deleuze
5152
* @since 2.5
5253
* @see #setUrl
5354
* @see TilesConfigurer
5455
*/
5556
public class TilesView extends AbstractUrlBasedView {
5657

58+
private boolean alwaysInclude = false;
59+
60+
61+
/**
62+
* Specify whether to always include the view rather than forward to it.
63+
* <p>Default is "false". Switch this flag on to enforce the use of a
64+
* Servlet include, even if a forward would be possible.
65+
* @see TilesViewResolver#setAlwaysInclude(Boolean)
66+
* @since 4.1.2
67+
*/
68+
public void setAlwaysInclude(boolean alwaysInclude) {
69+
this.alwaysInclude = alwaysInclude;
70+
}
71+
5772
@Override
5873
public boolean checkResource(final Locale locale) throws Exception {
5974
TilesContainer container = ServletUtil.getContainer(getServletContext());
@@ -85,6 +100,9 @@ protected void renderMergedOutputModel(
85100

86101
exposeModelAsRequestAttributes(model, request);
87102
JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));
103+
if (this.alwaysInclude) {
104+
ServletUtil.setForceInclude(request, true);
105+
}
88106
container.render(getUrl(), request, response);
89107
}
90108

spring-webmvc-tiles2/src/main/java/org/springframework/web/servlet/view/tiles2/TilesViewResolver.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.servlet.view.tiles2;
1818

19+
import org.springframework.web.servlet.view.AbstractUrlBasedView;
1920
import org.springframework.web.servlet.view.UrlBasedViewResolver;
2021

2122
/**
@@ -30,6 +31,7 @@
3031
* a non-null View object if the template was actually found.
3132
*
3233
* @author Juergen Hoeller
34+
* @author Sebastien Deleuze
3335
* @since 3.0
3436
* @see #setViewClass
3537
* @see #setPrefix
@@ -39,10 +41,24 @@
3941
*/
4042
public class TilesViewResolver extends UrlBasedViewResolver {
4143

44+
private Boolean alwaysInclude;
45+
46+
4247
public TilesViewResolver() {
4348
setViewClass(requiredViewClass());
4449
}
4550

51+
/**
52+
* Specify whether to always include the view rather than forward to it.
53+
* <p>Default is "false". Switch this flag on to enforce the use of a
54+
* Servlet include, even if a forward would be possible.
55+
* @see TilesView#setAlwaysInclude
56+
* @since 4.1.2
57+
*/
58+
public void setAlwaysInclude(Boolean alwaysInclude) {
59+
this.alwaysInclude = alwaysInclude;
60+
}
61+
4662
/**
4763
* Requires {@link TilesView}.
4864
*/
@@ -51,4 +67,13 @@ protected Class<?> requiredViewClass() {
5167
return TilesView.class;
5268
}
5369

70+
@Override
71+
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
72+
TilesView view = (TilesView) super.buildView(viewName);
73+
if (this.alwaysInclude != null) {
74+
view.setAlwaysInclude(this.alwaysInclude);
75+
}
76+
return view;
77+
}
78+
5479
}

spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles3/TilesView.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.apache.tiles.TilesContainer;
2525
import org.apache.tiles.access.TilesAccess;
2626
import org.apache.tiles.renderer.DefinitionRenderer;
27+
import org.apache.tiles.request.AbstractRequest;
2728
import org.apache.tiles.request.ApplicationContext;
2829
import org.apache.tiles.request.Request;
2930
import org.apache.tiles.request.render.Renderer;
@@ -46,6 +47,7 @@
4647
* @author Nicolas Le Bas
4748
* @author mick semb wever
4849
* @author Rossen Stoyanchev
50+
* @author Sebastien Deleuze
4951
* @since 3.2
5052
*/
5153
public class TilesView extends AbstractUrlBasedView {
@@ -54,6 +56,8 @@ public class TilesView extends AbstractUrlBasedView {
5456

5557
private boolean exposeJstlAttributes = true;
5658

59+
private boolean alwaysInclude = false;
60+
5761
private ApplicationContext applicationContext;
5862

5963

@@ -73,6 +77,17 @@ protected void setExposeJstlAttributes(boolean exposeJstlAttributes) {
7377
this.exposeJstlAttributes = exposeJstlAttributes;
7478
}
7579

80+
/**
81+
* Specify whether to always include the view rather than forward to it.
82+
* <p>Default is "false". Switch this flag on to enforce the use of a
83+
* Servlet include, even if a forward would be possible.
84+
* @see TilesViewResolver#setAlwaysInclude(Boolean)
85+
* @since 4.1.2
86+
*/
87+
public void setAlwaysInclude(boolean alwaysInclude) {
88+
this.alwaysInclude = alwaysInclude;
89+
}
90+
7691
@Override
7792
public void afterPropertiesSet() throws Exception {
7893
super.afterPropertiesSet();
@@ -84,7 +99,6 @@ public void afterPropertiesSet() throws Exception {
8499
}
85100
}
86101

87-
88102
@Override
89103
public boolean checkResource(final Locale locale) throws Exception {
90104
HttpServletRequest servletRequest = null;
@@ -109,6 +123,9 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
109123
if (this.exposeJstlAttributes) {
110124
JstlUtils.exposeLocalizationContext(new RequestContext(request, getServletContext()));
111125
}
126+
if (this.alwaysInclude) {
127+
request.setAttribute(AbstractRequest.FORCE_INCLUDE_ATTRIBUTE_NAME, true);
128+
}
112129

113130
Request tilesRequest = createTilesRequest(request, response);
114131
this.renderer.render(getUrl(), tilesRequest);

spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles3/TilesViewResolver.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,20 @@
2727
* @author Nicolas Le Bas
2828
* @author Rossen Stoyanchev
2929
* @author Juergen Hoeller
30+
* @author Sebastien Deleuze
3031
* @since 3.2
3132
*/
3233
public class TilesViewResolver extends UrlBasedViewResolver {
3334

3435
private Renderer renderer;
3536

37+
private Boolean alwaysInclude;
38+
3639

3740
public TilesViewResolver() {
3841
setViewClass(requiredViewClass());
3942
}
4043

41-
42-
/**
43-
* Requires {@link TilesView}.
44-
*/
45-
@Override
46-
protected Class<?> requiredViewClass() {
47-
return TilesView.class;
48-
}
49-
5044
/**
5145
* Set the {@link Renderer} to use. If not specified, a default
5246
* {@link org.apache.tiles.renderer.DefinitionRenderer} will be used.
@@ -56,13 +50,34 @@ public void setRenderer(Renderer renderer) {
5650
this.renderer = renderer;
5751
}
5852

53+
/**
54+
* Specify whether to always include the view rather than forward to it.
55+
* <p>Default is "false". Switch this flag on to enforce the use of a
56+
* Servlet include, even if a forward would be possible.
57+
* @see TilesView#setAlwaysInclude
58+
* @since 4.1.2
59+
*/
60+
public void setAlwaysInclude(Boolean alwaysInclude) {
61+
this.alwaysInclude = alwaysInclude;
62+
}
63+
64+
/**
65+
* Requires {@link TilesView}.
66+
*/
67+
@Override
68+
protected Class<?> requiredViewClass() {
69+
return TilesView.class;
70+
}
5971

6072
@Override
6173
protected TilesView buildView(String viewName) throws Exception {
6274
TilesView view = (TilesView) super.buildView(viewName);
6375
if (this.renderer != null) {
6476
view.setRenderer(this.renderer);
6577
}
78+
if (this.alwaysInclude != null) {
79+
view.setAlwaysInclude(this.alwaysInclude);
80+
}
6681
return view;
6782
}
6883

spring-webmvc/src/test/java/org/springframework/web/servlet/view/tiles3/TilesViewTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020

21+
import org.apache.tiles.request.AbstractRequest;
2122
import org.apache.tiles.request.Request;
2223
import org.apache.tiles.request.render.Renderer;
2324
import org.junit.Before;
@@ -36,6 +37,7 @@
3637
* Test fixture for {@link TilesView}.
3738
*
3839
* @author mick semb wever
40+
* @author Sebastien Deleuze
3941
*/
4042
public class TilesViewTests {
4143

@@ -72,12 +74,25 @@ public void setUp() throws Exception {
7274
}
7375

7476
@Test
75-
public void testRender() throws Exception {
77+
public void render() throws Exception {
7678
Map<String, Object> model = new HashMap<String, Object>();
7779
model.put("modelAttribute", "modelValue");
7880
view.render(model, request, response);
7981
assertEquals("modelValue", request.getAttribute("modelAttribute"));
8082
verify(renderer).render(eq(VIEW_PATH), isA(Request.class));
8183
}
8284

85+
@Test
86+
public void alwaysIncludeDefaults() throws Exception {
87+
view.render(new HashMap<String, Object>(), request, response);
88+
assertNull(request.getAttribute(AbstractRequest.FORCE_INCLUDE_ATTRIBUTE_NAME));
89+
}
90+
91+
@Test
92+
public void alwaysIncludeEnabled() throws Exception {
93+
view.setAlwaysInclude(true);
94+
view.render(new HashMap<String, Object>(), request, response);
95+
assertTrue((Boolean)request.getAttribute(AbstractRequest.FORCE_INCLUDE_ATTRIBUTE_NAME));
96+
}
97+
8398
}

0 commit comments

Comments
 (0)