Do I really need web.xml for a Servlet based Java web application?

后端 未结 7 992
无人共我
无人共我 2020-12-03 06:26

I haven\'t been working on real world web projects. At university we used both Servlets and Spring for Java web development. In both projects we were given web.xml files alr

相关标签:
7条回答
  • 2020-12-03 07:24

    Another way (Spring 3.1+) -

    An abstract base class implementation of WebApplicationInitializer named AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by simply overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration -

    public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
    
        @Override
        protected WebApplicationContext createRootApplicationContext() {
            return null;
        }
    
        @Override
        protected WebApplicationContext createServletApplicationContext() {
            XmlWebApplicationContext cxt = new XmlWebApplicationContext();
            cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
            return cxt;
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题