SpringMVC概述
三层架构
- 表现层:负责数据展示
- 业务层:负责业务员处理
- 数据层:负责数据操作
- SpringMVC属于表现层应用技术(简单可以理解为Servlet升级)
MVC
-
MVC(Model View Controller ),一种用于设计创建Web应用程序表现层的模式
- Model (模型):数据模型,用于封装数据
- View (视图) : 页面视图,用于展示数据
- jsp
- html
- Controller ( 控制器):处理用户交互的调度器,用于根据用户需求处理程序逻辑
- Servlet
- SpringMVC
SpringMVC简介
- SpringMVC是一种基于Java实现MVC模型的轻量级Web框架
SpringMVC优点
- 使用简单
- 性能突出(相比现有的框架技术)
- 灵活性强
SPringleMVC入门案列
- 导入依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
- 定义表现层业务处理器Controller,并配置成Spring的bean(等于Servlet)
@Controller
public class UserController {
public void save(){
System.out.println("user mvc controller is running...");
}
}
- web.xml中配置SpringMVC核心控制器,用于将请求转发到对应的具体业务处理器Controller中(等同于Servlet配置)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 编写SpringMVC配置文件
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.itheima"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
- 设定具体Controller的访问路径(等同于Servlet在Web.xml中的配置)和返回页面
@Controller
public class UserController {
@RequestMapping("/save")
public void save(){
System.out.println("user mvc controller is running...");
return "success.jsp";
} }
入门案例工作流程分析
- 服务器启动
- 加载Web.xml中的DispatcherServlet
- 读取spring-mvc.xml中的配置,加载所有com.itheima包中所有标记为bean的类
- 读取bean中方法上方标注@RequestMapping的内容
- 处理请求
- DispatcherServlet配置拦截所有请求 : /
- 使用请求路径与所有加载的@RequestMapping的内容进行比对
- 执行对应的方法
- 根据方法的返回值在webapp目录中查找对应的页面并展示
SpringMVC技术架构图
医院看病流程
SpringMVC流程
- DispatcherServlet:前端控制器,是整体流程控制的中心,由调用其他组件处理用户的请求,有效的降低了组件间的耦合性.
- HandlerMapping : 处理器映射器,负责根据用户请求找到对应的Handler处理器
- Handler : 处理器,业务处理的核心类,通常有开发者编写,描述具体的业务
- HandlAdapter:处理器适配器,通过它对处理器进行执行
- View Resolver : 视图解析器,将处理结果生成View视图
- View : 视图,最终产出结果,常用视图如jsp,html
基础配置
Controller加载控制
- SpringMVC的处理器对应的bean必须按照规范格式开发,避免加入无效的bean可用过加载过滤器进行包含设定或排除设定,表现层bean标注通常设定为@Controller
<context:component-scan base-package="com.itheima">
<context:include-filter
type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
@ComponentScan(
value="com.itheima",
excludeFilters =
@ComponentScan.Filter(
type= FilterType.ANNOTATION,
classes=Controller.class)
)
bean加载控制说明
- 业务层与数据层bean加载由Spring控制,参照spring加载方式
- 表现层bean加载有SpringMVC单独控制
- 表现层处理器bean使用注解@Controller声明
- bean加载控制使用包含性过滤器
- 过滤器类型为通过注解进行过滤
- 过滤的注解名称为Controller
静态资源加载
- 核心控制器拦截的是所有请求,需要对静态资源请求进行放行,通过配置放行资源实现
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
- 使用简化格式可以放行所有普通资源调用,无需一一枚举
<mvc:default-servlet-handler />
中文乱码处理
- SpringMVC提供专用的中文字符过滤器,用于处理乱码问题
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- tomcat依赖加入字符支持
<uriEncoding>UTF-8</uriEncoding>
注解驱动(了解即可)
- 使用注解形式转化SpringMVC核心配置文件为配置类
@Configuration
@ComponentScan(value="com.itheima",
excludeFilters = @ComponentScan.Filter(
type= FilterType.ANNOTATION,
classes=Controller.class
)
)
public class SpringMvcConfiguration {
}
- 基于Servlet3.0规范,自定义Servlet容器初始化配置类,加载SpringMVC核心配置类
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfiguration.class);
return ctx;
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
- bean加载过滤(注解版)
@Configuration
@ComponentScan(
value = "com.itheima.controller",
includeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
)
)
public class SpringMvcConfiguration {
}
- 静态资源加载过滤(注解版)
- 配置类实现WebMvcConfigurer接口,覆盖addResourceHandlers方法,在其中对具体的资源进行设定
public class SpringMvcConfiguration implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
}
}
-
- 或覆盖configureDefaultServletHandling方法,使用Servlet默认过滤规则
public class SpringMvcConfiguration implements WebMvcConfigurer {
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
- 中文乱码处理(注解版)
- Servlet3.0规范启动服务器时做的工作通过实现ServletContainerInitializer接口,在onStartup方法中完成,不包括监听器注册,过滤器注册等
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
//调用父类的onStartup
super.onStartup(servletContext);
//1.创建字符集过滤器对象
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
//2.设置使用的字符集
characterEncodingFilter.setEncoding("UTF-8");
//3.添加到容器(它不是ioc容器,而是ServletContainer)
FilterRegistration.Dynamic registration =
servletContext.addFilter("characterEncodingFilter", characterEncodingFilter);
//4.添加映射
registration.addMappingForUrlPatterns(
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD,DispatcherType.INCLUDE),false,"/*");
}
}
- 使用纯注解格式时在配置类加上@EnableWebMvc注解,否则会有无法识别图片以及其他静态资源