Skip to content
This repository
Browse code

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.
commit 213d2c742d472e602defcde801dd118e098d73c6 1 parent cc9a08f
Chris Beams authored
9  pom.xml
@@ -254,9 +254,9 @@
254 254
 
255 255
 		<!-- Servlet -->
256 256
 		<dependency>
257  
-			<groupId>javax.servlet</groupId>
  257
+			<groupId>org.mortbay.jetty</groupId>
258 258
 			<artifactId>servlet-api</artifactId>
259  
-			<version>2.5</version>
  259
+			<version>3.0.20100224</version>
260 260
 			<scope>provided</scope>
261 261
 		</dependency>
262 262
 		<dependency>
@@ -521,6 +521,9 @@
521 521
 				<groupId>org.apache.maven.plugins</groupId>
522 522
 				<artifactId>maven-war-plugin</artifactId>
523 523
 				<version>2.1.1</version>
  524
+				<configuration>
  525
+					<failOnMissingWebXml>false</failOnMissingWebXml>
  526
+				</configuration>
524 527
 			</plugin>			
525 528
 			<!-- Tomcat 7 Plugin -->
526 529
 			<plugin>
@@ -631,4 +634,4 @@
631 634
 		</snapshotRepository>
632 635
 	</distributionManagement>
633 636
 	
634  
-</project>
  637
+</project>
89  src/main/java/com/springsource/greenhouse/config/GreenhouseWebAppInitializer.java
... ...
@@ -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
+}
95  src/main/webapp/WEB-INF/web.xml
... ...
@@ -1,95 +0,0 @@
1  
-<?xml version="1.0" encoding="UTF-8"?>
2  
-<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3  
-	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4  
-	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
5  
-
6  
-	<context-param>
7  
-    	<param-name>contextClass</param-name>
8  
-    	<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
9  
-	</context-param>
10  
-
11  
-	<context-param>
12  
-    	<param-name>contextConfigLocation</param-name>
13  
-    	<param-value>com.springsource.greenhouse.config</param-value>
14  
-	</context-param>
15  
-	
16  
-	<context-param>
17  
-    	<param-name>spring.profiles.default</param-name>
18  
-    	<param-value>embedded</param-value>
19  
-	</context-param>
20  
-	
21  
-	<!-- Creates the Spring Container shared by all Servlets and Filters -->
22  
-	<listener>
23  
-		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
24  
-	</listener>
25  
-
26  
-	<!-- Allows attributes to be accessed on the next request -->
27  
-	<filter>
28  
-		<filter-name>flashMapFilter</filter-name>
29  
-		<filter-class>org.springframework.web.flash.FlashMapFilter</filter-class>
30  
-	</filter>
31  
-
32  
-	<filter-mapping>
33  
-		<filter-name>flashMapFilter</filter-name>
34  
-		<url-pattern>/*</url-pattern>
35  
-	</filter-mapping>
36  
-
37  
-	<!-- Enables support for DELETE and PUT request methods with web browser clients -->
38  
-	<filter>
39  
-		<filter-name>hiddenHttpMethodFilter</filter-name>
40  
-		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
41  
-	</filter>
42  
-
43  
-	<filter-mapping>
44  
-		<filter-name>hiddenHttpMethodFilter</filter-name>
45  
-		<url-pattern>/*</url-pattern>
46  
-	</filter-mapping>
47  
-
48  
-	<!-- Secures the application -->
49  
-	<filter>
50  
-		<filter-name>securityFilter</filter-name>
51  
-		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
52  
-		<init-param>
53  
-			<param-name>targetBeanName</param-name>
54  
-			<param-value>springSecurityFilterChain</param-value>
55  
-		</init-param>
56  
-	</filter>
57  
-
58  
-	<filter-mapping>
59  
-		<filter-name>securityFilter</filter-name>
60  
-		<url-pattern>/*</url-pattern>
61  
-	</filter-mapping>
62  
-
63  
-	<!-- H2 Database Console for managing the app's database -->
64  
-	<servlet>
65  
-		<servlet-name>H2Console</servlet-name>
66  
-		<servlet-class>org.h2.server.web.WebServlet</servlet-class>
67  
-		<init-param>
68  
-			<param-name>-webAllowOthers</param-name>
69  
-			<param-value>true</param-value>
70  
-		</init-param>
71  
-		<load-on-startup>2</load-on-startup>
72  
-	</servlet>
73  
-
74  
-	<servlet-mapping>
75  
-		<servlet-name>H2Console</servlet-name>
76  
-		<url-pattern>/admin/h2/*</url-pattern>
77  
-	</servlet-mapping>
78  
-
79  
-	<!-- Handles requests into the application -->
80  
-	<servlet>
81  
-		<servlet-name>appServlet</servlet-name>
82  
-		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
83  
-		<init-param>
84  
-			<param-name>contextConfigLocation</param-name>
85  
-			<param-value></param-value>
86  
-		</init-param>
87  
-		<load-on-startup>1</load-on-startup>
88  
-	</servlet>
89  
-
90  
-	<servlet-mapping>
91  
-		<servlet-name>appServlet</servlet-name>
92  
-		<url-pattern>/</url-pattern>
93  
-	</servlet-mapping>
94  
-
95  
-</web-app>

1 note on commit 213d2c7

Pyrolistical

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.
Something went wrong with that request. Please try again.