往期文章:
Spring 学习总结笔记【一、快速入门】
Spring 学习总结笔记【二、IoC-控制反转】
Spring 学习总结笔记【三、注解开发】
Spring 学习总结笔记【四、整合Junit】
Spring 学习总结笔记【五、配置数据源】
Spring 学习总结笔记【六、整合Mybatis】
Spring 学习总结笔记【七、AOP面向切面编程】
Spring 学习总结笔记【八、集成Web环境】
一、SpringMVC概述
- SpringMVC技术与Servlet技术功能等同,均属于web开发技术,可把SpringMVC看成Servlet的升级。
SpringMVC
是一种基于Java实现的MVC设计模型
的请求驱动类型的轻量级Web框架
,属于SpringFrameWork的后续产品,已经融合在Spring Web Flow 中。SpringMVC
是目前最主流的MVC框架之一,并且随着Spring3.0的发布,全面超越Struts,成为最优秀的MVC框架。它通过一套注解,使一个简单的Java类可以成为处理请求的控制器,并且无需实现任何接口。同时它还支持RESTful
编程风格的请求。
优点:
- 使用简单,开发便捷(相比于Servlet)
原生Servlet:
使用SpringMVC:
二、入门案例
导入SpringMVC坐标与Servlet坐标
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
创建Controller和业务方法 使用注解将controller放到Spring容器并配置访问路径
@Controller
public class UserController {
// 设置当前操作访问路径
@RequestMapping("/save")
// 设置当前操作返回值类型
@ResponseBody
public String save(){
System.out.println("user save ....");
return "{'info':'springMvc'}";
}
}
1. 基于XML配置
① 在web.xml配置SpringMVC的核心控制器
<?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_4_0.xsd"
version="4.0">
<!-- 配置SpringMVc的前端控制器 -->
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
② 编写核心配置文件(此处命名为spring-mvc.xml),配置组件扫描
<?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"
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">
<!-- SpringMVC只管理 com.tyt.controller 包下的类,两句效果相同-->
<!--<context:component-scan base-package="com.tyt.controller"/>-->
<context:component-scan base-package="com.tyt">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
2. 基于注解
① 编写SpringMVC核心控制器配置类
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer{
// 加载SpringMVC容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext app = new AnnotationConfigWebApplicationContext();
// 注册SpringMVC配置类
app.register(SpringMvcConfig.class);
return app;
}
// 加载Spring容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
// 此处只需用到SpringMVC容器,因此Spring容器可直接返回null
return null;
}
// 设置哪些请求归属SpringMVC处理
@Override
protected String[] getServletMappings() {
// 定义所有的请求都通过 SpringMVC
return new String[]{"/"};
}
}
或(简化上述开发):
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
// 加载SpringMVC容器配置
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
// 加载Spring容器配置
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{};
}
// 设置哪些请求归属SpringMVC处理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
② 初始化SpringMVC环境(同Spring环境),设定SpringMVC加载对象的bean
@Configuration
// SpringMVC只管理 com.tyt下的controller包下的类,两句效果相同
// @ComponentScan("com.tyt.controller")
@ComponentScan(value = "com.tyt",
includeFilters= @ComponentScan.Filter(type = FilterType.ANNOTATION,
classes = Controller.class))
public class SpringMvcConfig {
}
运行效果:
下期文章: