Home  >  Article  >  Java  >  Detailed introduction to SpringMVC workflow (with code)

Detailed introduction to SpringMVC workflow (with code)

不言
不言forward
2018-10-15 15:37:3910577browse

This article brings you a detailed introduction to the SpringMVC workflow (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

SpringMVC workflow:

Detailed introduction to SpringMVC workflow (with code)

##Detailed picture:

Detailed introduction to SpringMVC workflow (with code)

SpringMVC workflow description

1. User to server When sending a request, the request is obtained by the Spring front-end controller DispatcherServlet, as shown in the detailed figure in the first step

2. DispatcherServlet parses the request URL (for example, we send a request with the following URL

http: //localhost:8080/SpringMVC/hello.action), you will get the request resource identifier (URI, equivalent to hello.action above). Then according to the URI, the handler mapper (HandlerMapping) is called to obtain all related objects configured by the Handler (including the Handler object and the interceptor corresponding to the Handler object), and finally returned in the form of a HandlerExecutionChain object.

3.DispatcherServlet gets the Handler returned above and selects a suitable HandlerAdapter. (Note: If the HandlerAdapter is successfully obtained, the preHandler(...) method of the interceptor will start to be executed at this time)

4. After selecting the appropriate HandlerAdapter, the Handler will start to be executed. During the process of filling in the Handler's parameters, Spring will do some extra work for you according to the configuration (we don't need to take care of it):

EG:

HttpMessageConveter: Will request the message (such as Json , xml and other data) into an object, and convert the object into the specified response information

Data conversion: Convert the request message to data. For example, String is converted into Integer, Double, etc.

Data radicalization: Format the data of the request message. Such as converting strings into formatted numbers or formatted dates, etc.

Data verification: Verify the validity of the data (length, format, etc.), and store the verification results in BindingResult or Error

5 . After the execution of the Handler is completed, a ModelAndView object (including the view name or the view name and the model) is returned to the DispatcherServlet

6. Based on the returned ModelAndView object, select a suitable ViewResolver and return it to the DispatcherServlet;

7. ViewResolver combines Model and View to render the view

8. Finally, return the view rendering result to the client

Component noun explanation:


1. DispatcherServlet: The front-end controller

is equivalent to the previous Controller. It is the center of the entire process and is responsible for calling other components

2.HandlerMapping: The processor mapper

is responsible for calling according to the request Find the Handler (processor). Different mappings can be implemented in springMVC according to different mappers, such as xml configuration method, annotation method, interface method, etc.

3.Handler: back-end controller

Specific user requests are processed under the control of the front-end controller, so developers are generally required to develop according to needs.

4.HandlerAdapter: Processor Adapter

Handler processing can execute multiple types of processors. This is the application of the adapter mode.

5.ViewResolver: View resolver

is responsible for generating the view view from the processing results. Developers can develop view

as needed. Getting started program (detailed steps): After understanding After the springMVC workflow, let's implement our entry program according to the process steps. The steps are as follows:

1. Import our spring framework package (generally, you can download it from the official website and the 4.2 version is more commonly used)

2. Configure the front-end controller in web.xml

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- 让servlet随服务启动 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

Here is our interception method:

1. Intercept URLs with fixed suffix names: such as *.action, *.do

2. Intercept all: set to /, but this method will cause static files (css, js.jpg ) is intercepted and cannot be displayed normally, so this method requires special processing

Note: You cannot set all interceptions to /* This method is wrong because when an action is requested, it will be blocked again when the action jumps to jsp Interception, exception occurred: The mapping address

3 cannot be found according to the jsp path.Settingsspringmvc Configuration file

<servlet>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/springmvc.xml</param-value>
</init-param>
</servlet>

4.Development Processor

public class Hello implements Controller{
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List list = new ArrayList<>();
		list.add("one");
		list.add("two");
		ModelAndView mv = new ModelAndView();
		mv.addObject("list",list);
		return mv;
	}
}

5.在springmvc.xml中配置、

<!-- 配置适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 处理器映射器  -->
<!-- 根据bean的 name 查找Handler , 将action的URL 配置在bean的name中-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置处理器 -->
<bean name="/hello.action" class="com.mt.controller.Hello"></bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

6.视图开发  jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
asdfadfadfa
${list }
</body>
</html>

测试结果:   开启服务器后, 发送 http://localhost:8080/SpringMVC/hello.action的请求

页面显示:

Detailed introduction to SpringMVC workflow (with code)

对应理解springMVC  的流程即可。

The above is the detailed content of Detailed introduction to SpringMVC workflow (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete