revised WebApplicationContext lookup

This commit is contained in:
Juergen Hoeller 2009-04-19 22:28:31 +00:00
parent 4cf573ba98
commit 736169aa2a
3 changed files with 25 additions and 26 deletions

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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.web.servlet.support; package org.springframework.web.servlet.support;
import java.util.Locale; import java.util.Locale;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -79,10 +78,7 @@ public abstract class RequestContextUtils {
if (servletContext == null) { if (servletContext == null) {
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet request?"); throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet request?");
} }
webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
if (webApplicationContext == null) {
throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
}
} }
return webApplicationContext; return 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,16 +16,15 @@
package org.springframework.web.servlet.view.tiles2; package org.springframework.web.servlet.view.tiles2;
import javax.servlet.ServletRequest;
import org.apache.tiles.TilesException; import org.apache.tiles.TilesException;
import org.apache.tiles.context.TilesRequestContext; import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.PreparerFactory; import org.apache.tiles.preparer.PreparerFactory;
import org.apache.tiles.preparer.ViewPreparer; import org.apache.tiles.preparer.ViewPreparer;
import org.apache.tiles.servlet.context.ServletTilesApplicationContext; import org.apache.tiles.servlet.context.ServletTilesRequestContext;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.RequestContextUtils; import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
/** /**
* Abstract implementation of the Tiles2 {@link org.apache.tiles.preparer.PreparerFactory} * Abstract implementation of the Tiles2 {@link org.apache.tiles.preparer.PreparerFactory}
@ -41,20 +40,24 @@ import org.springframework.web.servlet.support.RequestContextUtils;
public abstract class AbstractSpringPreparerFactory implements PreparerFactory { public abstract class AbstractSpringPreparerFactory implements PreparerFactory {
public ViewPreparer getPreparer(String name, TilesRequestContext context) throws TilesException { public ViewPreparer getPreparer(String name, TilesRequestContext context) throws TilesException {
ServletRequest servletRequest = null; WebApplicationContext webApplicationContext = (WebApplicationContext) context.getRequestScope().get(
if (context.getRequest() instanceof ServletRequest) { DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
servletRequest = (ServletRequest) context.getRequest(); 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());
} }
ServletTilesApplicationContext tilesApplicationContext = null;
if (context instanceof ServletTilesApplicationContext) {
tilesApplicationContext = (ServletTilesApplicationContext) context;
}
if (servletRequest == null && tilesApplicationContext == null) {
throw new IllegalStateException("SpringBeanPreparerFactory requires either a " +
"ServletRequest or a ServletTilesApplicationContext to operate on");
}
WebApplicationContext webApplicationContext = RequestContextUtils.getWebApplicationContext(
servletRequest, tilesApplicationContext.getServletContext());
return getPreparer(name, webApplicationContext); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,7 +36,7 @@ public class SpringBeanPreparerFactory extends AbstractSpringPreparerFactory {
@Override @Override
protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException { protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException {
return (ViewPreparer) context.getBean(name, ViewPreparer.class); return context.getBean(name, ViewPreparer.class);
} }
} }