Spring MVC Learning (a)---front-end controller

Source: Internet
Author: User

The front controller in 1.spring MVC is Dsipatcherservlet, which is structured in the Spring MVC framework as follows:

2.DispatcherServlet is actually a servlet, which inherits the abstract class of HttpServlet.

First look at the location of the class:

In order to use it, you need to configure it in Web. XML in your application, as follows:

<servlet>        <Servlet-name>Dispatcher</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value></Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Dispatcher</Servlet-name>        <Url-pattern>/example/*</Url-pattern>    </servlet-mapping>

With this configuration, all requests in your application with/example will be intercepted by Dispatcherservlet.

There is another way to configure the container for 3.0++, which is to replace it with code:

 public  class  Mywebapplicationinitialzer implements   Webapplicationinitializer { public  void  Onstartup (ServletContext servletcontext) throws   servletexception { //  TODO auto-generated method stub  servletregistration.dynamic regist = Servletcontext.addservlet ( "Dispatcher", new   Dispatcherservlet ());        Regist.setloadonstartup ( 1 "/example/*" 

Sets the path for the map to start when the container is loaded.

3. Each dispatcherservlet has its own context (Webapplicationcontext), which inherits the beans in the context and can be rewritten on its own terms.

Here is an inheritance plot:

4. After configuring the Dispatcherservlet in Web. XML, the container goes back to Web-inf folder when it starts (default [servlet-name]- Servlet.xml) Dispatcherservlet-servlet.xml, parse file initialization inside the bean, etc.

5. In the actual application we generally do not use this default configuration, you only need to configure the following file in Web. XML, here is an example, replace the Param-value with your custom can:

<Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>/web-inf/root-context.xml</Param-value>    </Context-param><Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener><servlet>        <Servlet-name>Dispatcher</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value></Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Dispatcher</Servlet-name>        <Url-pattern>/*</Url-pattern>    </servlet-mapping>

6. Well, a generic simple spring MVC basic configuration is complete.

Spring MVC Learning (a)---front-end controller

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.