From 813295c4d17fa25e93fc68dd82850db65596ed4d Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 26 Jul 2013 11:55:43 +0300 Subject: [PATCH] initial work on replacing the web.xml --- .../java/org/baeldung/servlet/NewWebApp.java | 40 +++++++++++++++++++ .../java/org/baeldung/servlet/WebApp.java | 31 ++++++++++++++ .../webapp/WEB-INF/{web.xml => web_old.xml} | 0 3 files changed, 71 insertions(+) create mode 100644 spring-mvc-no-xml/src/main/java/org/baeldung/servlet/NewWebApp.java create mode 100644 spring-mvc-no-xml/src/main/java/org/baeldung/servlet/WebApp.java rename spring-mvc-no-xml/src/main/webapp/WEB-INF/{web.xml => web_old.xml} (100%) diff --git a/spring-mvc-no-xml/src/main/java/org/baeldung/servlet/NewWebApp.java b/spring-mvc-no-xml/src/main/java/org/baeldung/servlet/NewWebApp.java new file mode 100644 index 0000000000..a0181aedaa --- /dev/null +++ b/spring-mvc-no-xml/src/main/java/org/baeldung/servlet/NewWebApp.java @@ -0,0 +1,40 @@ +package org.baeldung.servlet; + +// +//public class NewWebApp extends AbstractAnnotationConfigDispatcherServletInitializer { +// +// public NewWebApp() { +// super(); +// } +// +// // API +// +// @Override +// public void onStartup(final ServletContext servletContext) throws ServletException { +// final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); +// root.setServletContext(servletContext); +// root.scan("org.baeldung.spring"); +// root.refresh(); +// +// final Dynamic servlet = servletContext.addServlet("mvc", new DispatcherServlet(root)); +// servlet.setLoadOnStartup(1); +// servlet.addMapping("/*"); +// } +// +// @Override +// protected Class[] getRootConfigClasses() { +// return null; +// } +// +// @Override +// protected Class[] getServletConfigClasses() { +// return new Class[] { ClientWebConfig.class }; +// } +// +// @Override +// protected String[] getServletMappings() { +// return new String[] { "/*" }; +// } +// +//} + diff --git a/spring-mvc-no-xml/src/main/java/org/baeldung/servlet/WebApp.java b/spring-mvc-no-xml/src/main/java/org/baeldung/servlet/WebApp.java new file mode 100644 index 0000000000..583d083c1c --- /dev/null +++ b/spring-mvc-no-xml/src/main/java/org/baeldung/servlet/WebApp.java @@ -0,0 +1,31 @@ +package org.baeldung.servlet; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration.Dynamic; + +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +public class WebApp implements WebApplicationInitializer { + + public WebApp() { + super(); + } + + // API + + @Override + public void onStartup(final ServletContext servletContext) throws ServletException { + final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); + root.setServletContext(servletContext); + root.scan("org.baeldung.spring"); + root.refresh(); + + final Dynamic servlet = servletContext.addServlet("mvc", new DispatcherServlet(root)); + servlet.setLoadOnStartup(1); + servlet.addMapping("/"); + } + +} diff --git a/spring-mvc-no-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-no-xml/src/main/webapp/WEB-INF/web_old.xml similarity index 100% rename from spring-mvc-no-xml/src/main/webapp/WEB-INF/web.xml rename to spring-mvc-no-xml/src/main/webapp/WEB-INF/web_old.xml