Browse Source

revised WebApplicationContext lookup

conversation
Juergen Hoeller 16 years ago
parent
commit
736169aa2a
  1. 8
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java
  2. 39
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/AbstractSpringPreparerFactory.java
  3. 4
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/SpringBeanPreparerFactory.java

8
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java

@ -1,5 +1,5 @@ @@ -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,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.web.servlet.support;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
@ -79,10 +78,7 @@ public abstract class RequestContextUtils { @@ -79,10 +78,7 @@ public abstract class RequestContextUtils {
if (servletContext == null) {
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet request?");
}
webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
if (webApplicationContext == null) {
throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
}
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}
return webApplicationContext;
}

39
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/AbstractSpringPreparerFactory.java

@ -1,5 +1,5 @@ @@ -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.
@ -16,16 +16,15 @@ @@ -16,16 +16,15 @@
package org.springframework.web.servlet.view.tiles2;
import javax.servlet.ServletRequest;
import org.apache.tiles.TilesException;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.PreparerFactory;
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.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}
@ -41,20 +40,24 @@ import org.springframework.web.servlet.support.RequestContextUtils; @@ -41,20 +40,24 @@ import org.springframework.web.servlet.support.RequestContextUtils;
public abstract class AbstractSpringPreparerFactory implements PreparerFactory {
public ViewPreparer getPreparer(String name, TilesRequestContext context) throws TilesException {
ServletRequest servletRequest = null;
if (context.getRequest() instanceof ServletRequest) {
servletRequest = (ServletRequest) context.getRequest();
}
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 = (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());
}
WebApplicationContext webApplicationContext = RequestContextUtils.getWebApplicationContext(
servletRequest, tilesApplicationContext.getServletContext());
return getPreparer(name, webApplicationContext);
}

4
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/SpringBeanPreparerFactory.java

@ -1,5 +1,5 @@ @@ -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.
@ -36,7 +36,7 @@ public class SpringBeanPreparerFactory extends AbstractSpringPreparerFactory { @@ -36,7 +36,7 @@ public class SpringBeanPreparerFactory extends AbstractSpringPreparerFactory {
@Override
protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException {
return (ViewPreparer) context.getBean(name, ViewPreparer.class);
return context.getBean(name, ViewPreparer.class);
}
}

Loading…
Cancel
Save