[SpringMVC] HelloSpringMVC-Introduction to development and detailed components

Understand SpringMVC

The traditional MVC is Model model/View view/Controller controller, and correspond to JavaBean/JSP/Servlet respectively. (For more MVC knowledge and illustrations, you can click here )

In the traditional MVC development model, the key is the preparation of Controller, that is, the preparation of Servlets one by one. These Servlets must directly or indirectly implement the Servlet interface and rewrite the doGet/doPost methods... Just imagine, when the business logic becomes more complex, there will be more and more Servlets, and it will become troublesome to write and maintain.

In the SpringMVC framework, the designer cleverly extracted the public behaviors of many Servlets into a packaged DispatcherServlet; and the unique behavior of each Servlet can be written in a class that does not need to implement any interface.

The following diagram may help understanding:

Insert picture description here

 
 

SpringMVC development steps and code implementation

First of all, it is clear that the requirements implemented by SpringMVC are: receiving client requests, processing business logic, and jumping to page views

To make a long story short:

① Import SpringMVC related coordinates

② Configure SpringMVC core controller DispathcerServlet (in web.xml)

③ Create Controller class and JSP view page, and use annotation to configure address mapping

④ Configure SpringMVC core configuration file spring-mvc.xml (just for component scanning)

⑤ The client initiates a request (test)

Code:

① Import SpringMVC related coordinates

<!-- 仅供参考 -->
<!-- 关键是spring-webmvc -->

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
</dependency>
<dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.2.1</version>
</dependency>

② Configure SpringMVC core controller DispathcerServlet (in web.xml)

<!-- 配置SpringMVC前端控制器 -->
<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 告诉SpringMVC框架配置文件的位置 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!-- web项目启动时加载一次 -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

③ Create Controller class and JSP view page, and use annotation to configure address mapping

@Controller
public class UserController {
    
    

    @RequestMapping("/hello")
    public String sayHi() {
    
    
        System.out.println("Hello >_<");
        return "index.jsp";
    }

}

④ Configure the SpringMVC core configuration file spring-mvc.xml (currently only for component scanning)

<!-- 组件扫描(只扫描controller包即可) -->
<context:component-scan base-package="com.samarua.controller" />

⑤ The client initiates a request (test)

>>> 浏览器URL输入:http://localhost:8080/hello

 
 

About @RequestMapping annotation

作用:
	建立资源路径到某个类的某个方法的映射(代替了之前学的每个Servlet上的url注解)

位置:
	- 类上。
	- 方法上。
	-"多级目录"的思想,类上的一级目录与方法上的二级目录进行"拼接",组成完整的资源路径

参数:
	- value: 资源路径,这是一个字符串
	- method: 请求的方式,常见的有RequestMethod.GET/POST/PUT/DELETE
	- param: 用于指定参数需要满足的条件,这是一个表达式数组

举例:
	@RequestMapping(value = "/hello", method = RequestMethod.GET, params = {
    
    "username", "!sex", "isLoli=true", "age!=12"})

 
 

About the configuration of the suffixes and suffixes of the view parser in spring-mvc.xml

<!-- 配置视图解析器前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views" />
    <property name="suffix" value=".jsp" />
</bean>

<!-- 默认前缀为"redirect:" -->

 
 

SpringMVC code flow diagram

Insert picture description here

The main function of the server-side engine is to parse the request resource path; to convert the request/response information of the Http protocol and the request/response object .

 
 

SpringMVC components and processes

Illustration
Insert picture description here

Process

① The user sends a request to the front controller DispatcherServlet (dispatching core)

② DispatcherServlet receives the request to call the HandlerMapping processor mapper

③ The processor mapper finds a specific processor, generates an execution chain that reaches the processor, and returns it to DispatcherServlet

④ DispatcherServlet calls the HandlerAdapter processor adapter

⑤ HandlerAdapter finds a specific processor according to the execution chain

⑥ After the processor is executed, it returns to ModelAndView (model and view)

⑦ HandlerAdapter returns the processor execution result ModelAndView to DispatcherServlet

⑧ DispatcherServlet passes ModelAndView to ViewReslover view parser

⑨ ViewReslover returns to the specific View after parsing

⑩ DispatcherServlet renders the view according to the View (that is, fills the model data into the view)

⑪ Final response

Component

Front controller: DispatcherServlet

When the user request reaches the front controller, it is equivalent to reaching C in the MVC pattern. DispatcherServlet is the center of the entire process control. It calls other components to process user requests. The existence of DispatcherServlet reduces the coupling between components.

Processor mapper: HandlerMapping

​HandlerMapping can map to the real processor according to the resource path provided by the user request, and return this execution chain

Processor adapter: HandlerAdapter

The actual execution of the processor through the HandlerAdapter

Processor: Handler

That is Controller, that is, the controller, that is, the specific processing logic.

View resolver: View Resolver

View Resolver first parses the logical view name into a physical view name, that is, a specific page address, and then generates a View view object, and finally renders the View and displays the result to the user through the page.

View: View

Including jstlView, freemarkerView, pdfView, etc., the most commonly used view is jsp.

 
 
 
 

 
 
 
 

 
 
 
 

More >_<

Guess you like

Origin blog.csdn.net/m0_46202073/article/details/114265394