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