Add an alwaysInclude property to TilesViewResolver

Issue: SPR-12374
This commit is contained in:
Sebastien Deleuze
2014-10-31 17:26:21 +01:00
parent 01382b8ff0
commit cfa3d358d5
5 changed files with 105 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -48,12 +48,27 @@ import org.springframework.web.servlet.view.AbstractUrlBasedView;
* {@link TilesConfigurer} bean definition in the application context.
*
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 2.5
* @see #setUrl
* @see TilesConfigurer
*/
public class TilesView extends AbstractUrlBasedView {
private boolean alwaysInclude = false;
/**
* Specify whether to always include the view rather than forward to it.
* <p>Default is "false". Switch this flag on to enforce the use of a
* Servlet include, even if a forward would be possible.
* @see TilesViewResolver#setAlwaysInclude(Boolean)
* @since 4.1.2
*/
public void setAlwaysInclude(boolean alwaysInclude) {
this.alwaysInclude = alwaysInclude;
}
@Override
public boolean checkResource(final Locale locale) throws Exception {
TilesContainer container = ServletUtil.getContainer(getServletContext());
@@ -85,6 +100,9 @@ public class TilesView extends AbstractUrlBasedView {
exposeModelAsRequestAttributes(model, request);
JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));
if (this.alwaysInclude) {
ServletUtil.setForceInclude(request, true);
}
container.render(getUrl(), request, response);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2014 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.
@@ -16,6 +16,7 @@
package org.springframework.web.servlet.view.tiles2;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
/**
@@ -30,6 +31,7 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver;
* a non-null View object if the template was actually found.
*
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 3.0
* @see #setViewClass
* @see #setPrefix
@@ -39,10 +41,24 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver;
*/
public class TilesViewResolver extends UrlBasedViewResolver {
private Boolean alwaysInclude;
public TilesViewResolver() {
setViewClass(requiredViewClass());
}
/**
* Specify whether to always include the view rather than forward to it.
* <p>Default is "false". Switch this flag on to enforce the use of a
* Servlet include, even if a forward would be possible.
* @see TilesView#setAlwaysInclude
* @since 4.1.2
*/
public void setAlwaysInclude(Boolean alwaysInclude) {
this.alwaysInclude = alwaysInclude;
}
/**
* Requires {@link TilesView}.
*/
@@ -51,4 +67,13 @@ public class TilesViewResolver extends UrlBasedViewResolver {
return TilesView.class;
}
@Override
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
TilesView view = (TilesView) super.buildView(viewName);
if (this.alwaysInclude != null) {
view.setAlwaysInclude(this.alwaysInclude);
}
return view;
}
}