Spring Web MVC module in the Spring Framework 1. Overview
-
- Spring Web MVC is a module in the spring framework
- Spring Web MVC implements the MVC architecture pattern of the web and can be used to develop web sites
- Spring Web MVC implements the principles of Web sites, such as:
2. Steps to develop a Web app using Spring Web MVC
Step1: Introducing the Spring Web MVC module into your project
Step2: Configure Dispatcherservlet in the Spring Web MVC module to tell him which requests to intercept
Step3:
Related knowledge points in 3.spring Web MVC 3.1 about Dispatcherservlet in spring Web MVC
-
Dispatcherservlet is a core part of the Spring Web MVC module, and Dispatcherservlet has the following features
the DispatcherServlet
is an actual Servlet
(it inherits HttpServlet
from the base class),
To DispatcherServlet
be able to intercept a user's request, you need to do some configuration, such as using URL mapping to map user requests toDispatcherServlet。可以有多种方法来使得用户请求被映射到DispatcherServlet上,
方法一,直接继承spring MVC 模块的WebApplicationInitializer接口,来配置spring MVC模块的DispatcherServlet,使其可以接收到用户请求
Mywebapplicationinitializer.java
The user request is mapped to the dispatcherservlet of the Spring Web MVC module in a URL way, allowing the user request to be processed by Dispatcherservlet being forwarded to the Controller. And get processed results as feedback to the user
- In the following example all requests starting with would be
/example
handled by the DispatcherServlet
instance named example
.
/*
1)WebApplicationInitializer
is a interface provided by Spring MVC This ensures your code-based configuration is detected and automatically used Itialize any Servlet 3 container.
2)
*/
Public classMywebapplicationinitializerImplementsWebapplicationinitializer {@Override Public voidonstartup (ServletContext container) {servletregistration.dynamic registration= Container.addservlet ("Example",NewDispatcherservlet ()); Registration.setloadonstartup (1); Registration.addmapping ("/example/*"); }}
Configure the URL mapping using the method described above (that is, Java code), handing the user request to the Dispatcherservlet to distribute to the corresponding controller, Traditionally, the effect of using the Web. xml file To configure the mapping is the same, as the above code in this example is equivalent to the configuration code for the following Web. XML (in traditional mode, the user request URL is configured using Web. config to allow user requests to be intercepted by the servlet, such as by the spring Dispatcherservlet Interception of web MVC))
- In traditional mode, the mapping of the request URL and servlet is configured in Web. Xml as follows:
<!--The above code is equivalent to the Java code and the traditional mode under the Web. xml File
Is the servlet that/example/* the user request to the Web app (in the example, Dispatcherservlet in Spring web MVC) to handle
Let the servlet give the received user request to the controller layer corresponding to the handling method to handle-
<Web-app> <servlet> <Servlet-name>Example</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Example</Servlet-name> <Url-pattern>/example/*</Url-pattern> </servlet-mapping></Web-app>
Method two, inheritance AbstractAnnotationConfigDispatcherServletInitializer
(is the implementation class of the method mentioned in the Webapplicationinitializer interface)
Spring Framework------>version4.3.5.relaese----->reference Documentation Learning Experience----->spring Spring Web MVC module in the framework