Spring (4): SpringMVC frame

First, the first to introduce a simple framework SpringBoot

1.1 What is SpringBoot

Spring is the essence SpringBoot, it removes the tedious Spring XML configuration, the configuration can be only a small amount.

1.2, SpringBoot advantages

(1) to the configuration of the simple development

(2) provides embedded HTTP server, such as Tomcat, you can easily develop and test

(3) provides a command line interface (CLI) tools for development and testing

1.3, RESTful style presentation

Traditional MVC model development will be directly returned to the client a view, but RESTful Web service returns JSON form (front and rear ends of the splitter)

 

Two, Spring MVC framework introduced

2.1 What is the MVC pattern

MVC can be understood as a design pattern

SpringMVC request frame is driven around Servlect design, the request to the controller, then the model object dispatcher view to show the results of the request (which is the DispatcherServlet core class, it is a Servlect, top implement Servlect interface)

 

2.2, SpringMVC use

DispatcherServlect needs to be configured in the web.xml hey, you need to configure Spring listener ContextLoaderListener

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>
<servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet
	</servlet-class>
	<!-- 如果不设置init-param标签,则必须在/WEB-INF/下创建xxx-servlet.xml文件,其中xxx是servlet-name中配置的名称。 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/springmvc-servlet.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>

2.3, SpringMVC works

(1) The client sends a request

(2) -> front end controller DispatcherServlect accept client requests

(3) -> processor mapping found HandlerMapping resolution request corresponding Handler

(4) -> HandlerAdapter will be called the real processor request (in accordance with Handler adapter mode ), corresponding service logic and processing

(5) -> processor returns a model view ModelAndView 

(6) -> view parser

(7) -> Returns a view object

(8) -> front controller rendering data DispatcherServlect

(9) to give view object returned to the client

As shown below:

SpringMVC operating principle

 

2.4, SpringMVC important component description

(1) front-end controller DispatcherServlect (no need to develop, framework)

Action: Spring MVC entry function, in response to receiving the request result, corresponds to the " central processor ", corresponding to the MVC C , which will call the user requests other components, reducing coupling between components.

(2) the HandlerMapping processor mapper (no need to develop, framework)

Role: Find Handler processor (Controller) based on the URL requested by the user.

(3) Processor Adapter HandlerAdapter (no need to develop, framework)

Action: according to specific rules by the processor to perform Handler, is the application of the adapter mode, the processor may be more types of expansion performed by the adapter.

(4) Processor Handler ( Development needs )

(Ie Controller Controller)

DispatcherServlect front controller similar to the controller of the rear end, under the control of DispatcherServlect, Han dler specific user request is processed .

(5) Resolver view resolver View (no need to develop, framework)

Role: a view to resolve, according to the logic of resolving view names into real view (view), responsible for handling the results generated View view object, and finally through notification page displayed to the user rendering results View .

(6) View View ( need to develop )

View is an interface to achieve class supports different types of View

Note: Processor View Handler and the need to develop their own

 

Previous: the Spring

Published 52 original articles · won praise 116 · views 50000 +

Guess you like

Origin blog.csdn.net/RuiKe1400360107/article/details/103610245