Skip to content

Instantly share code, notes, and snippets.

@frow
Created May 18, 2012 21:33
Show Gist options
  • Save frow/2727706 to your computer and use it in GitHub Desktop.
Save frow/2727706 to your computer and use it in GitHub Desktop.
The Web App Initializer
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(WebMvcConfig.class);
ServletRegistration.Dynamic appServlet = servletContext.addServlet(
"appServlet", new DispatcherServlet(mvcContext));
appServlet.setLoadOnStartup(1);
Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException(
"'appServlet' cannot be mapped to '/' under Tomcat versions <= 7.0.14");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment