1.Spring MVC is based on MODEL2 implementation, the overall framework process such as (image from Baidu):
The ①web container receives an HTTP request, and if it matches the Dispatcherservlet request mapping Path (Web. xml), the container is given dispatcherservlet processing.
The ②dispatcherservlet finds the handler to process the request based on the requested information and the configuration of the handlermapping (similar to the routing feature).
③handler adapter encapsulates the Handler and calls the Handler method using a uniform method
After the ④ processor completes the business logic, it returns a Modelandview to Dispatcherservlet,modelandview that contains the view logical name and the model data information.
⑤modelandview contains the logical view name, Dispatcherservlet the parsing of the logical view to the real view by Viewresolver
⑥ The View object is real, Dispatcherservlet uses this view object to render the view of the model data in Modelandview.
The ⑦ client eventually gets HTML or PDF, and so on.
2. Configure Dispatcherservlet
<Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:/conf/applicationcontext.xml</Param-value> </Context-param> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <servlet> <Servlet-name>Viewspace</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Viewspace</Servlet-name> <Url-pattern>*.html</Url-pattern> </servlet-mapping>
Note: Configuring Dispatcherservlet, named Viewspace, automatically loads the <servlet-name>-servlet.xml configuration file by default.
A Web. XML can be configured with multiple Dispatcherservlet to handle different requests through servlet-mapping.
If you do not want to load the default <servlet-name>-servlet.xml, you can also display the specified:
<!--Spring MVC Servlet - <servlet> <Servlet-name>Springservlet</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/spring-mvc.xml</Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet>
01-spring MVC Overview and Configuration Dispatcherservlet