updated Tiles support for Tiles 2.1 compatibility (SPR-5411)

This commit is contained in:
Juergen Hoeller 2009-07-08 13:29:39 +00:00
parent 928855e47a
commit 2b13afd891
7 changed files with 149 additions and 94 deletions

View File

@ -52,11 +52,13 @@
conf="compile->compile"/>
<dependency org="org.apache.poi" name="com.springsource.org.apache.poi" rev="3.0.2.FINAL"
conf="optional, poi->compile"/>
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles" rev="2.0.5"
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles" rev="2.1.2.osgi"
conf="optional, tiles->compile"/>
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.core" rev="2.0.5.osgi"
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.core" rev="2.1.2.osgi"
conf="optional, tiles->compile"/>
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.jsp" rev="2.0.5"
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.jsp" rev="2.1.2"
conf="optional, tiles->compile"/>
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.servlet" rev="2.1.2"
conf="optional, tiles->compile"/>
<dependency org="org.apache.velocity" name="com.springsource.org.apache.velocity" rev="1.5.0"
conf="optional, velocity->compile"/>

View File

@ -43,20 +43,11 @@ public abstract class AbstractSpringPreparerFactory implements PreparerFactory {
WebApplicationContext webApplicationContext = (WebApplicationContext) context.getRequestScope().get(
DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (webApplicationContext == null) {
/* as of Tiles 2.1:
webApplicationContext = (WebApplicationContext) context.getApplicationContext().getApplicationScope().get(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (webApplicationContext == null) {
throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
}
*/
if (!(context instanceof ServletTilesRequestContext)) {
throw new IllegalStateException(
getClass().getSimpleName() + " requires a ServletTilesRequestContext to operate on");
}
ServletTilesRequestContext servletRequestContext = (ServletTilesRequestContext) context;
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(
servletRequestContext.getServletContext());
}
return getPreparer(name, webApplicationContext);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 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.
@ -17,11 +17,13 @@
package org.springframework.web.servlet.view.tiles2;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.jsp.context.JspTilesRequestContext;
import org.apache.tiles.locale.impl.DefaultLocaleResolver;
import org.apache.tiles.servlet.context.ServletTilesRequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;
@ -42,8 +44,12 @@ public class SpringLocaleResolver extends DefaultLocaleResolver {
@Override
public Locale resolveLocale(TilesRequestContext context) {
if (context.getRequest() instanceof HttpServletRequest) {
return RequestContextUtils.getLocale((HttpServletRequest) context.getRequest());
if (context instanceof ServletTilesRequestContext) {
return RequestContextUtils.getLocale(((ServletTilesRequestContext) context).getRequest());
}
else if (context instanceof JspTilesRequestContext) {
PageContext pc = ((JspTilesRequestContext) context).getPageContext();
return RequestContextUtils.getLocale((HttpServletRequest) pc.getRequest());
}
else {
return super.resolveLocale(context);

View File

@ -23,17 +23,18 @@ import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.TilesException;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.context.ChainedTilesContextFactory;
import org.apache.tiles.definition.UrlDefinitionsFactory;
import org.apache.tiles.context.AbstractTilesApplicationContextFactory;
import org.apache.tiles.definition.DefinitionsFactory;
import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
import org.apache.tiles.evaluator.el.ELAttributeEvaluator;
import org.apache.tiles.factory.TilesContainerFactory;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.jsp.context.JspTilesContextFactory;
import org.apache.tiles.preparer.BasicPreparerFactory;
import org.apache.tiles.servlet.context.ServletTilesContextFactory;
import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
import org.apache.tiles.servlet.context.ServletUtil;
import org.apache.tiles.servlet.context.wildcard.WildcardServletTilesApplicationContextFactory;
import org.apache.tiles.startup.BasicTilesInitializer;
import org.apache.tiles.startup.TilesInitializer;
import org.apache.tiles.web.util.ServletContextAdapter;
import org.springframework.beans.factory.DisposableBean;
@ -49,7 +50,9 @@ import org.springframework.web.context.ServletContextAware;
* mechanism for JSP-based web applications.
*
* <p>The TilesConfigurer simply configures a TilesContainer using a set of files
* containing definitions, to be accessed by {@link TilesView} instances.
* containing definitions, to be accessed by {@link TilesView} instances. This is a
* Spring-based alternative (for usage in Spring configuration) to the Tiles-provided
* {@link org.apache.tiles.web.startup.TilesListener} (for usage in <code>web.xml</code>).
*
* <p>TilesViews can be managed by any {@link org.springframework.web.servlet.ViewResolver}.
* For simple convention-based view resolution, consider using {@link TilesViewResolver}.
@ -69,12 +72,12 @@ import org.springframework.web.context.ServletContextAware;
* &lt;/property>
* &lt;/bean></pre>
*
* The values in the list are the actual files containing the definitions.
* The values in the list are the actual Tiles XML files containing the definitions.
*
* @author Juergen Hoeller
* @since 2.5
* @see TilesView
* @see org.springframework.web.servlet.view.UrlBasedViewResolver
* @see TilesViewResolver
*/
public class TilesConfigurer implements ServletContextAware, InitializingBean, DisposableBean {
@ -86,24 +89,16 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
public TilesConfigurer() {
this.tilesPropertyMap.put(
TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
TilesContainerFactory.class.getName());
this.tilesPropertyMap.put(
TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM,
ChainedTilesContextFactory.class.getName());
this.tilesPropertyMap.put(
TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM,
UrlDefinitionsFactory.class.getName());
this.tilesPropertyMap.put(
TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM,
this.tilesPropertyMap.put(AbstractTilesApplicationContextFactory.APPLICATION_CONTEXT_FACTORY_INIT_PARAM,
WildcardServletTilesApplicationContextFactory.class.getName());
this.tilesPropertyMap.put(TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM,
BasicPreparerFactory.class.getName());
this.tilesPropertyMap.put(
ChainedTilesContextFactory.FACTORY_CLASS_NAMES,
ServletTilesContextFactory.class.getName() + "," + JspTilesContextFactory.class.getName());
this.tilesPropertyMap.put(
UrlDefinitionsFactory.LOCALE_RESOLVER_IMPL_PROPERTY,
this.tilesPropertyMap.put(DefinitionsFactory.LOCALE_RESOLVER_IMPL_PROPERTY,
SpringLocaleResolver.class.getName());
this.tilesPropertyMap.put(TilesContainerFactory.ATTRIBUTE_EVALUATOR_INIT_PARAM,
ELAttributeEvaluator.class.getName());
this.tilesPropertyMap.put(TilesContainerFactory.CONTAINER_FACTORY_MUTABLE_INIT_PARAM,
Boolean.toString(false));
}
@ -117,7 +112,7 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
if (logger.isInfoEnabled()) {
logger.info("TilesConfigurer: adding definitions [" + defs + "]");
}
this.tilesPropertyMap.put(BasicTilesContainer.DEFINITIONS_CONFIG, defs);
this.tilesPropertyMap.put(DefinitionsFactory.DEFINITIONS_CONFIG, defs);
}
}
@ -190,24 +185,23 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
/**
* Creates and exposes a TilesContainer for this web application.
* Creates and exposes a TilesContainer for this web application,
* delegating to the TilesInitializer.
* @throws TilesException in case of setup failure
* @see #createTilesInitializer()
*/
public void afterPropertiesSet() throws TilesException {
TilesContainer container = createTilesContainer(this.servletContext);
TilesAccess.setContainer(this.servletContext, container);
ServletContextAdapter adaptedContext = new ServletContextAdapter(new DelegatingServletConfig());
createTilesInitializer().initialize(new ServletTilesApplicationContext(adaptedContext));
}
/**
* Create a TilesContainer for this web application.
* @param context this web application's ServletContext
* @return the TilesContainer to expose
* @throws TilesException in case of setup failure
* Creates a new instance of {@link org.apache.tiles.startup.BasicTilesInitializer}.
* Override it to use a different initializer.
* @see org.apache.tiles.web.startup.TilesListener#createTilesInitializer()
*/
protected TilesContainer createTilesContainer(ServletContext context) throws TilesException {
ServletContextAdapter adaptedContext = new ServletContextAdapter(new DelegatingServletConfig());
TilesContainerFactory factory = TilesContainerFactory.getFactory(adaptedContext);
return factory.createContainer(adaptedContext);
protected TilesInitializer createTilesInitializer() {
return new BasicTilesInitializer();
}
/**
@ -215,7 +209,7 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
* @throws TilesException in case of cleanup failure
*/
public void destroy() throws TilesException {
TilesAccess.setContainer(this.servletContext, null);
ServletUtil.setContainer(this.servletContext, null);
}

View File

@ -16,14 +16,20 @@
package org.springframework.web.servlet.view.tiles2;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
import org.apache.tiles.servlet.context.ServletTilesRequestContext;
import org.apache.tiles.servlet.context.ServletUtil;
import org.springframework.web.servlet.support.JstlUtils;
import org.springframework.web.servlet.support.RequestContext;
@ -49,9 +55,21 @@ import org.springframework.web.util.WebUtils;
public class TilesView extends AbstractUrlBasedView {
@Override
public boolean checkResource() throws Exception {
TilesContainer container = TilesAccess.getContainer(getServletContext());
return container.isValidDefinition(getUrl());
public boolean checkResource(final Locale locale) throws Exception {
TilesContainer container = ServletUtil.getContainer(getServletContext());
if (!(container instanceof BasicTilesContainer)) {
// Cannot check properly - let's assume it's there.
return true;
}
BasicTilesContainer basicContainer = (BasicTilesContainer) container;
TilesApplicationContext appContext = new ServletTilesApplicationContext(getServletContext());
TilesRequestContext requestContext = new ServletTilesRequestContext(appContext, null, null) {
@Override
public Locale getRequestLocale() {
return locale;
}
};
return (basicContainer.getDefinitionsFactory().getDefinition(getUrl(), requestContext) != null);
}
@Override
@ -59,7 +77,7 @@ public class TilesView extends AbstractUrlBasedView {
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ServletContext servletContext = getServletContext();
TilesContainer container = TilesAccess.getContainer(servletContext);
TilesContainer container = ServletUtil.getContainer(servletContext);
if (container == null) {
throw new ServletException("Tiles container is not initialized. " +
"Have you added a TilesConfigurer to your web application context?");

View File

@ -20,7 +20,7 @@ Import-Template:
org.apache.commons.fileupload.*;version="[1.2.0, 2.0.0)";resolution:=optional,
org.apache.commons.logging.*;version="[1.1.1, 2.0.0)";resolution:=optional,
org.apache.poi.*;version="[3.0.2.FINAL, 4.0.0)";resolution:=optional,
org.apache.tiles.*;version="[2.0.5, 3.0.0)";resolution:=optional,
org.apache.tiles.*;version="[2.1.2, 3.0.0)";resolution:=optional,
org.apache.velocity.*;version="[1.5.0, 2.0.0)";resolution:=optional,
org.apache.velocity.tools.*;version="[1.4.0, 3.0.0)";resolution:=optional,
org.codehaus.jackson.*;version="[1.0.0, 1.1.0)";resolution:=optional,

View File

@ -128,39 +128,6 @@
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles/2.0.5/com.springsource.org.apache.tiles-2.0.5.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles/2.0.5/com.springsource.org.apache.tiles-sources-2.0.5.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.core/2.0.5.osgi/com.springsource.org.apache.tiles.core-2.0.5.osgi.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.core/2.0.5.osgi/com.springsource.org.apache.tiles.core-sources-2.0.5.osgi.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.jsp/2.0.5/com.springsource.org.apache.tiles.jsp-2.0.5.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.jsp/2.0.5/com.springsource.org.apache.tiles.jsp-sources-2.0.5.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
@ -313,6 +280,83 @@
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles/2.1.2.osgi/com.springsource.org.apache.tiles-2.1.2.osgi.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles/2.1.2.osgi/com.springsource.org.apache.tiles-sources-2.1.2.osgi.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.core/2.1.2.osgi/com.springsource.org.apache.tiles.core-2.1.2.osgi.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.core/2.1.2.osgi/com.springsource.org.apache.tiles.core-sources-2.1.2.osgi.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.jsp/2.1.2/com.springsource.org.apache.tiles.jsp-2.1.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.jsp/2.1.2/com.springsource.org.apache.tiles.jsp-sources-2.1.2.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.servlet/2.1.2/com.springsource.org.apache.tiles.servlet-2.1.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.apache.tiles/com.springsource.org.apache.tiles.servlet/2.1.2/com.springsource.org.apache.tiles.servlet-sources-2.1.2.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-sources-1.0.0.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson.mapper/1.0.0/com.springsource.org.codehaus.jackson.mapper-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson.mapper/1.0.0/com.springsource.org.codehaus.jackson.mapper-sources-1.0.0.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.mozilla.javascript/com.springsource.org.mozilla.javascript/1.7.0.R2/com.springsource.org.mozilla.javascript-1.7.0.R2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.mozilla.javascript/com.springsource.org.mozilla.javascript/1.7.0.R2/com.springsource.org.mozilla.javascript-sources-1.7.0.R2.jar!/" />
</SOURCES>
</library>
</orderEntry>
</component>
<component name="copyright">
<Base>