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

Commit 213d2c7

Browse files
committed
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.
1 parent cc9a08f commit 213d2c7

File tree

3 files changed

+95
-98
lines changed

3 files changed

+95
-98
lines changed

pom.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@
254254

255255
<!-- Servlet -->
256256
<dependency>
257-
<groupId>javax.servlet</groupId>
257+
<groupId>org.mortbay.jetty</groupId>
258258
<artifactId>servlet-api</artifactId>
259-
<version>2.5</version>
259+
<version>3.0.20100224</version>
260260
<scope>provided</scope>
261261
</dependency>
262262
<dependency>
@@ -521,6 +521,9 @@
521521
<groupId>org.apache.maven.plugins</groupId>
522522
<artifactId>maven-war-plugin</artifactId>
523523
<version>2.1.1</version>
524+
<configuration>
525+
<failOnMissingWebXml>false</failOnMissingWebXml>
526+
</configuration>
524527
</plugin>
525528
<!-- Tomcat 7 Plugin -->
526529
<plugin>
@@ -631,4 +634,4 @@
631634
</snapshotRepository>
632635
</distributionManagement>
633636

634-
</project>
637+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.springsource.greenhouse.config;
18+
19+
import java.util.Set;
20+
21+
import javax.servlet.ServletContext;
22+
import javax.servlet.ServletException;
23+
import javax.servlet.ServletRegistration;
24+
25+
import org.springframework.web.WebApplicationInitializer;
26+
import org.springframework.web.context.ContextLoaderListener;
27+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
28+
import org.springframework.web.context.support.GenericWebApplicationContext;
29+
import org.springframework.web.filter.DelegatingFilterProxy;
30+
import org.springframework.web.filter.HiddenHttpMethodFilter;
31+
import org.springframework.web.flash.FlashMapFilter;
32+
import org.springframework.web.servlet.DispatcherServlet;
33+
34+
/**
35+
* Code-based alternative to web.xml for use within Servlet 3.0+ environments. See
36+
* {@link WebApplicationInitializer} Javadoc for complete details.
37+
*
38+
* @author Chris Beams
39+
*/
40+
public class GreenhouseWebAppInitializer implements WebApplicationInitializer {
41+
42+
/**
43+
* Register and configure all Servlet container components necessary to power the
44+
* Greenhouse web application.
45+
*/
46+
@Override
47+
public void onStartup(ServletContext sc) throws ServletException {
48+
System.out.println("GreenhouseWebAppInitializer.onStartup()");
49+
50+
// Create the 'root' Spring application context
51+
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
52+
root.scan("com.springsource.greenhouse.config");
53+
root.getEnvironment().setDefaultProfiles("embedded");
54+
55+
// Manages the lifecycle of the root application context
56+
sc.addListener(new ContextLoaderListener(root));
57+
58+
// Allows attributes to be accessed on the next request
59+
sc.addFilter("flashMapFilter", FlashMapFilter.class)
60+
.addMappingForUrlPatterns(null, false, "/*");
61+
62+
// Enables support for DELETE and PUT request methods with web browser clients
63+
sc.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class)
64+
.addMappingForUrlPatterns(null, false, "/*");
65+
66+
// Secures the application
67+
sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
68+
.addMappingForUrlPatterns(null, false, "/*");
69+
70+
// Handles requests into the application
71+
ServletRegistration.Dynamic appServlet =
72+
sc.addServlet("appServlet", new DispatcherServlet(new GenericWebApplicationContext()));
73+
appServlet.setLoadOnStartup(1);
74+
Set<String> mappingConflicts = appServlet.addMapping("/");
75+
if (!mappingConflicts.isEmpty()) {
76+
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " +
77+
"to an existing mapping. This is a known issue under Tomcat versions " +
78+
"<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
79+
}
80+
81+
// H2 Database Console for managing the app's database
82+
ServletRegistration.Dynamic h2Servlet =
83+
sc.addServlet("H2Console", org.h2.server.web.WebServlet.class);
84+
h2Servlet.setInitParameter("webAllowOthers", "true");
85+
h2Servlet.setLoadOnStartup(2);
86+
h2Servlet.addMapping("/admin/h2/*");
87+
}
88+
89+
}

src/main/webapp/WEB-INF/web.xml

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)