能够知道SpringMVC的优点
能够编写SpringMVC入门案例
能够使用PostMan发送请求
能够掌握普通类型参数传递
能够掌握POJO类型参数传递
能够掌握json数据参数传递
能够掌握响应json数据
能够掌握rest风格快速开发
能够完成基于restful页面数据交互案例
SpringMVC框架有什么优点?
SpringMVC是一种基于Java实现MVC模型的轻量级Web框架
优点
在Controller中如何定义访问路径,如何响应数据?
1 创建web工程(Maven结构)
2 设置tomcat服务器,加载web工程(tomcat插件)
3 导入坐标(SpringMVC+Servlet)
4 定义处理请求的功能类(UserController)
5 编写SpringMVC配置类,加载处理请求的Bean。
6 加载SpringMVC配置,并设置SpringMVC请求拦截的路径
xxxxxxxxxx
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
xxxxxxxxxx
<dependencies>
<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>
注意事项:
xxxxxxxxxx
//定义表现层控制器bean
public class UserController {
//设置映射路径为/save,即外部访问路径
"/save") (
//设置当前操作返回结果为指定json数据(本质上是一个字符串信息)
public String save(){
System.out.println("user save ...");
return "{'info':'springmvc'}";
}
}
==注意事项:==
对于SpringMVC而言,Controller方法返回值默认表示要跳转的页面,没有对应的页面就会报错。如果不想跳转页面而是响应数据,那么就需要在方法上使用@ResponseBody注解。
xxxxxxxxxx
//springmvc配置类,本质上还是一个spring配置类
"com.itheima.controller") (
public class SpringMvcConfig {
}
x
//web容器配置类
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//加载springmvc配置类,产生springmvc容器(本质还是spring容器)
protected WebApplicationContext createServletApplicationContext() {
//初始化WebApplicationContext对象
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//加载指定配置类
ctx.register(SpringMvcConfig.class);
return ctx;
}
//设置由springmvc控制器处理的请求映射路径
protected String[] getServletMappings() {
return new String[]{"/"};
}
//加载spring配置类
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
xxxxxxxxxx
public class UserController {
}
xxxxxxxxxx
"/save") (
public void save(){
System.out.println("user save ...");
}
注意:其实@RequestMapping注解还可以写到类上面,笔记后面会介绍到。
xxxxxxxxxx
"/save") (
public String save(){
System.out.println("user save ...");
return "{'info':'springmvc'}";
}
AbstractDispatcherServletInitializer类是SpringMVC提供的快速初始化Web3.0容器的抽象类
AbstractDispatcherServletInitializer提供三个接口方法供用户实现
xxxxxxxxxx
//加载springmvc配置类,产生springmvc容器(本质还是spring容器)
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
xxxxxxxxxx
//设置由springmvc控制器处理的请求映射路径
protected String[] getServletMappings() {
return new String[]{"/"};
}
xxxxxxxxxx
//加载spring配置类
protected WebApplicationContext createRootApplicationContext() {
return null;
}
一次性工作
多次工作
因为功能不同,如何避免Spring错误的加载到SpringMVC的bean?
SpringMVC相关bean(表现层bean)
Spring控制的bean
SpringMVC相关bean加载控制
Spring相关bean加载控制
xxxxxxxxxx
value = "com.itheima", (
excludeFilters = .Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
)
)
public class SpringConfig {
}
属性
xxxxxxxxxx
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
简化格式
xxxxxxxxxx
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class}
};
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
}
Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件。
作用:常用于进行接口测试
特征
双击资料中的“Postman-win64-8.3.1-Setup.exe”即可自动按照,打开之后需要注册,如果底部有如下链接,可以点击跳过注册
注意:第一次请求需要创建一个新的目录,后面就不需要创建新目录,直接保存到已经创建好的目录即可。