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


Replace web.xml with GreenhouseWebAppInitializer
Browse files Browse the repository at this point in the history
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

See WebApplicationInitializer Javadoc for complete details.
  • Loading branch information
cbeams committed May 30, 2011
1 parent cc9a08f commit 213d2c7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 98 deletions.
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@

<!-- Servlet -->
Expand Down Expand Up @@ -521,6 +521,9 @@
<!-- Tomcat 7 Plugin -->
Expand Down Expand Up @@ -631,4 +634,4 @@

Original file line number Diff line number Diff line change
@@ -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
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.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.
public void onStartup(ServletContext sc) throws ServletException {

// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();

// 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()));
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");

// 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");

95 changes: 0 additions & 95 deletions src/main/webapp/WEB-INF/web.xml

This file was deleted.

1 comment on commit 213d2c7

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a clean sample project demonstrating xml-free-configuration:

Please sign in to comment.