03-workflow

1. 时序图

1

2. 原理图

2

1. DispatcherServlet

  • 前端控制器:相当于MVC中的C,DispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户请求,降低了组件之间的耦合性

2. HandlerMapping

  • 处理器映射器:根据请求找到Handler即处理器,SpringMVC提供了不同的映射器实现不同的映射方式
    • eg:配置文件方式、实现接口方式、注解方式等

3. Handler

  • 处理器(自定义的Controller处理单元):开发中要编写的具体业务控制器。由DispatcherServlet把用户请求转发到Handler

4. HandlerAdapter

  • 处理器适配器:通过HandlerAdapter对处理器进行执行。适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行
3

5. View Resolver

  • 视图解析器:负责将处理结果生成View视图
  • View Resolver首先将逻辑视图名解析成物理视图名,即具体的页面地址。再生成View视图对象,最后对View进行渲染,将处理结果通过页面展示给用户

6. View

  • 视图:SpringMVC框架提供了很多对View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最常用视图就是jsp
  • 一般情况下需要通过页面标签、页面模板技术将模型数据通过页面展示给用户,需要根据业务需求开发具体页面

3. annotation-driven

  • 在SpringMVC的各个组件中,《处理器映射器》、《处理器适配器》、《视图解析器》称为SpringMVC的三大组件
  • <mvc:annotation-driven>自动加载RequestMappingHandlerMapping(处理映射器)、RequestMappingHandlerAdapter(处理适配器)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!-- 配置Spring包扫描 -->
    <context:component-scan base-package="com.listao.mvc.controller"/>


    <!-- 1. 配置处理器映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!-- 2. 配置处理器适配器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    <!--
        一个注解替换上面的两个配置
            会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter两个Bean
    -->
    <mvc:annotation-driven/>

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

















 
 
 
 




 







  1. HandlerMapping作用
    • 实现类RequestMappingHandlerMapping,处理@RequestMapping注解,并将其注册到请求映射表中
  2. HandlerAdapter作用
    • 实现类RequestMappingHandlerAdapter,处理请求的适配器,确定调用哪个类的哪个方法,并且构造方法参数,返回值

4. 静态资源放行

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!-- 配置Spring包扫描 -->
    <context:component-scan base-package="com.listao.mvc.controller"/>


    <!-- 配置处理器映射器 -->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
    <!-- 配置处理器适配器 -->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
    <!--
        一个注解替换上面的两个配置
            会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter两个Bean
    -->
    <mvc:annotation-driven/>

    <!-- 静态资源放行 -->
    <mvc:resources mapping="/static/**" location="/static/"/>

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




























 







<%-- 解决中文乱码 --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>static.jsp</title>

   <link rel="stylesheet" type="text/css" href="static/css/aaa.css"/>
   <script src="static/js/bbb.js"></script>
</head>
<body>
<h2>static.jsp</h2>

<hr/>

<img src="static/img/icon.jpg" alt="ooxx"/>

</body>
</html>






 
 






 



image-20231230104821541