- 前端控制器:相当于MVC中的C,
DispatcherServlet
是整个流程控制的中心,由它调用其它组件处理用户请求,降低了组件之间的耦合性
- 处理器映射器:根据请求找到
Handler
即处理器,SpringMVC提供了不同的映射器实现不同的映射方式
- 处理器(自定义的Controller处理单元):开发中要编写的具体业务控制器。由
DispatcherServlet
把用户请求转发到Handler
- 处理器适配器:通过
HandlerAdapter
对处理器进行执行。适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行
- 视图解析器:负责将处理结果生成View视图
View Resolver
首先将逻辑视图名解析成物理视图名,即具体的页面地址。再生成View视图对象,最后对View进行渲染,将处理结果通过页面展示给用户
- 视图:SpringMVC框架提供了很多对View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最常用视图就是jsp
- 一般情况下需要通过页面标签、页面模板技术将模型数据通过页面展示给用户,需要根据业务需求开发具体页面
- 在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
">
<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"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
HandlerMapping
作用 - 实现类
RequestMappingHandlerMapping
,处理@RequestMapping
注解,并将其注册到请求映射表中
HandlerAdapter
作用 - 实现类
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
">
<context:component-scan base-package="com.listao.mvc.controller"/>
<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>