Spring MVC configures multiple dispatcher and WebApplicationInitializer usage, springmvcdispatcher

Source: Internet
Author: User

Spring MVC configures multiple dispatcher and WebApplicationInitializer usage, springmvcdispatcher
SpringMVC can be configured as follows when configuring multiple dispatcher:

<!-- spring mvc start -->    <servlet>        <servlet-name>springMVC</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath*:/spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springMVC</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <servlet>        <servlet-name>SytDeveloper</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath*:/developer-spring-mvc.xml</param-value>        </init-param>        <load-on-startup>2</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>SytDeveloper</servlet-name>        <url-pattern>/developer/*</url-pattern>    </servlet-mapping>        <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.html</url-pattern>    </servlet-mapping>    <!-- spring mvc end -->

Note:

1. When viewResolver is configured in the developer-spring-mvc.xml, do not start with/developer/in <property name = "prefix">, otherwise the "404 Problem accessing" Problem will occur:

No mapping found for HTTP request with URI [/developer/html/index.jsp] in DispatcherServlet with name 'developerMvc'

Because DispatcherServlet treats the page path as a URI, rather than view Parsing

2. The SytDeveloper's url-pattern must be followed by *. If not, the DispatcherServlet named SpringMVC will be used for processing.

 

In addition, if you need multiple dispatcherservlets during multi-module development, you may need to modify the web. xml is cumbersome and reduces the random combination of modules. The idea is web. whether xml can include other xml files, but web. xml does not seem to allow include, so you can use WebApplicationInitializer for configuration. Note that it can be implemented only with Servlet 3.0 +. For details about WebApplicationInitializer, there are many details on the Internet. The main principle is that the container will automatically scan the implementation class of the ServletContainerInitializer interface, and then call the onStartup method. Spring provides an implementation class SpringServletContainerInitializer, the implementation class of WebApplicationInitializer can be automatically recognized and processed by SpringServletContainerInitializer. For more information, see the source code of these classes.

Example:

Configure SpringMVC in web. xml in the Code as follows:

 

/*** @ Title: FrameDefaultInitializer. java * @ package com. shuyuntu. initializer * @ description: TODO initializes the Frame configuration. You can initialize the configuration, such as filter, listener, and servlet * @ copyright: shuyuntu.com * @ author Zhang Shimin * @ date 9:28:03 on January 1, August 17, 2016 * @ version 1.0 */public class FrameDefaultInitializer implements WebApplicationInitializer {@ Override public void onStartup (letservletcontext ServletContext) throws ServletException {// TODO run to initialize servletContext. getServletRegistration ("default "). addMapping ("*. html "); // Add Spring mvc configuration XmlWebApplicationContext appContext = new XmlWebApplicationContext (); appContext. setConfigLocation ("classpath *:/spring-mvc.xml"); ServletRegistration. dynamic dispatcher = servletContext. addServlet ("springMvc", new DispatcherServlet (appContext); dispatcher. setLoadOnStartup (100); dispatcher. addMapping ("/");}}

 

Appendix: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/WebApplicationInitializer.html

 

By using code for configuration, we feel that the module and module are more flexible. You can directly configure the modules required by a platform to reference them in maven.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.