Skip to content
This repository has been archived by the owner on Aug 5, 2023. It is now read-only.

Commit

Permalink
switch to web.xml based initialization, split web and app contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Colyer committed Oct 2, 2012
1 parent e8aba41 commit 9c79147
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cargo.gradle
Expand Up @@ -7,14 +7,14 @@ dependencies {
}

cargo {
containerId = 'tomcat7x'
containerId = 'tomcat6x'
port = 8080
deployable {
context = name
}

local {
homeDir = file("$tomcat7")
homeDir = file("$tomcat6")
log = file('build/tomcat.log')
}
}
@@ -0,0 +1,18 @@
package org.springsource.samples.montyhall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Controller;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages = "org.springsource.samples.montyhall",
excludeFilters = {@Filter(Controller.class)})
public class AppConfig {


}
Expand Up @@ -9,7 +9,7 @@

@Configuration
@EnableWebMvc
@ComponentScan("org.springsource.samples.montyhall")
@ComponentScan("org.springsource.samples.montyhall.controller")
public class WebConfig {

@Bean
Expand Down
Expand Up @@ -10,6 +10,12 @@
import javax.servlet.ServletRegistration;
import java.util.Set;

/**
* For use with Servlet 3.0 and Tomcat 7.
* Cloud Foundry does not yet support Tomcat 7 so use web.xml instead when pushing to
* the 'spring' runtime on Cloud Foundry. Alternatively package the whole app including tomcat 7 as
* a standalone app.
*/
public class MontyHallWebAppInitializer implements WebApplicationInitializer {

@java.lang.Override
Expand Down
46 changes: 46 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- Use annotation-based configuration from the montyhall.config package -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.springsource.samples.montyhall.config.AppConfig</param-value>
</context-param>

<!-- Creates the Spring Application Context based on the above -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Main servlet -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.springsource.samples.montyhall.config.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>

</web-app>

0 comments on commit 9c79147

Please sign in to comment.