Permalink
Browse files

Replace web.xml with GreenhouseWebAppInitializer

Pom has been upgraded to servlet-api 3.0, and web.xml has been
eliminated completely in favor of Spring 3.1's new
WebApplicationInitializer SPI, which builds upon Servlet 3.0's
ServletContainerInitializers.

See WebApplicationInitializer Javadoc for complete details.
  • Loading branch information...
cbeams committed May 27, 2011
1 parent cc9a08f commit 213d2c742d472e602defcde801dd118e098d73c6
View
@@ -254,9 +254,9 @@
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<version>3.0.20100224</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -521,6 +521,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- Tomcat 7 Plugin -->
<plugin>
@@ -631,4 +634,4 @@
</snapshotRepository>
</distributionManagement>
</project>
</project>
@@ -0,0 +1,89 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.springsource.greenhouse.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.flash.FlashMapFilter;
import org.springframework.web.servlet.DispatcherServlet;
/**
* Code-based alternative to web.xml for use within Servlet 3.0+ environments. See
* {@link WebApplicationInitializer} Javadoc for complete details.
*
* @author Chris Beams
*/
public class GreenhouseWebAppInitializer implements WebApplicationInitializer {
/**
* Register and configure all Servlet container components necessary to power the
* Greenhouse web application.
*/
@Override
public void onStartup(ServletContext sc) throws ServletException {
System.out.println("GreenhouseWebAppInitializer.onStartup()");
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("com.springsource.greenhouse.config");
root.getEnvironment().setDefaultProfiles("embedded");
// Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root));
// Allows attributes to be accessed on the next request
sc.addFilter("flashMapFilter", FlashMapFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
// Enables support for DELETE and PUT request methods with web browser clients
sc.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
// Secures the application
sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
.addMappingForUrlPatterns(null, false, "/*");
// Handles requests into the application
ServletRegistration.Dynamic appServlet =
sc.addServlet("appServlet", new DispatcherServlet(new GenericWebApplicationContext()));
appServlet.setLoadOnStartup(1);
Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " +
"to an existing mapping. This is a known issue under Tomcat versions " +
"<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
}
// H2 Database Console for managing the app's database
ServletRegistration.Dynamic h2Servlet =
sc.addServlet("H2Console", org.h2.server.web.WebServlet.class);
h2Servlet.setInitParameter("webAllowOthers", "true");
h2Servlet.setLoadOnStartup(2);
h2Servlet.addMapping("/admin/h2/*");
}
}
@@ -1,95 +0,0 @@
<?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">
<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>com.springsource.greenhouse.config</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>embedded</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Allows attributes to be accessed on the next request -->
<filter>
<filter-name>flashMapFilter</filter-name>
<filter-class>org.springframework.web.flash.FlashMapFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>flashMapFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Enables support for DELETE and PUT request methods with web browser clients -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Secures the application -->
<filter>
<filter-name>securityFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- H2 Database Console for managing the app's database -->
<servlet>
<servlet-name>H2Console</servlet-name>
<servlet-class>org.h2.server.web.WebServlet</servlet-class>
<init-param>
<param-name>-webAllowOthers</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>H2Console</servlet-name>
<url-pattern>/admin/h2/*</url-pattern>
</servlet-mapping>
<!-- Handles requests into the application -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></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>
</web-app>

1 comment on commit 213d2c7

@Pyrolistical

This comment has been minimized.

Show comment
Hide comment
@Pyrolistical

Pyrolistical Nov 8, 2011

Here is a clean sample project demonstrating xml-free-configuration: http://pyrolistical.github.com/blog/2011/10/23/spring-without-xml/

Here is a clean sample project demonstrating xml-free-configuration: http://pyrolistical.github.com/blog/2011/10/23/spring-without-xml/

Please sign in to comment.