ok了家人们今天继续学习SpringMvc,let‘s go?
2.9 静态资源放行
编写类继承于 WebMvcConfigurationSupport ,重写
addResourceHandlers 方法,在类上添加 @Configuration 注
解。
- addResourceHandler映射的地址,/**表示包含子孙目录
- addResourceLocations表示物理存在的地址
- 四个静态目录都进行了映射
//当前类需要设置为配置类,并被扫描加载
@Configuration
public class SpringMvcSupport extends
WebMvcConfigurationSupport {
@Override
protected void
addResourceHandlers(ResourceHandlerRegistry
registry) {
//当访问/pages/xxxx时候,从/pages目录下查找
内容
registry.addResourceHandler("/pages/**").addRes
ourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResour
ceLocations("/js/");
registry.addResourceHandler("/css/**").addResou
rceLocations("/css/");
registry.addResourceHandler("/plugins/**").addR
esourceLocations("/plugins/");
}
}
2.10 JSON数据参数传递
- 添加json数据转换相关坐标
因为 Springmvc 默认用
MappingJacksonHttpMessageConverter 对 json 数据进行
转换,需要添加 jackson 依赖。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
- 开启自动转换json数据的支持
@Configuration
@ComponentScan({"com.lzw"})
@EnableWebMvc //开启SpringMVC JSON数据进行自动类型转换
public class SpringMvcConfig {
}
- @RequestBody
作用:形参注解,将请求体所包含的数据传递给请求参数
- 传递JSON数组
<h2>JSON数据参数传递--传递JSON数组</h2>
<input type="button" value="点我"
onclick="fn01()"/>
<script src="/js/axios-0.18.0.js"></script>
<script>
function fn01(){
var arrayJson=["张三","李四","王五"];
axios.post("http://localhost:8080/demo10",array
Json).then(function(response){
console.log(response.data);
});
}
</script>
@RequestMapping("/demo10")
@ResponseBody
public String demo10(@RequestBody
List<String> list){
System.out.println(list);
return "success";
}
- 传递JSON对象
json 数据与形参对象属性名相同,定义 POJO 类型形参即可接
收参数
<h2>JSON数据参数传递--传递JSON对象</h2>
<input type="button" value="点我"
onclick="fn02()"/>
<script src="/js/axios-0.18.0.js"></script>
<script>
function fn02(){
var objectJson=
{"id":1,"username":"张三","password":"123456"};
axios.post("http://localhost:8080/demo11",objec
tJson).then(function(response){
console.log(response.data);
});
}
</script>
@RequestMapping("/demo11")
@ResponseBody
public String demo11(@RequestBody User user)
{
System.out.println(user);
return "success";
}
- 传递JSON对象数组
json 数组数据与集合泛型属性名相同,定义 List 类型形参即可
接收参数
<h2>JSON数据参数传递--传递JSON对象数组</h2>
<input type="button" value="点我"
onclick="fn03()"/>
javascript"><script src="/js/axios-0.18.0.js"></script>
<script>
function fn03(){
var objectArray=[
{"id":1,"username":"张三","password":"123456"},
{"id":2,"username":"李四","password":"123456"},
];
axios.post("http://localhost:8080/demo12",objec
tArray).then(function(response){
console.log(response.data);
});
}
</script>
@RequestMapping("/demo12")
@ResponseBody
public String demo12(@RequestBody List<User>
list){
System.out.println(list);
return "success";
}
2.11 日期类型参数传递
相同的日期数据,可能会采用不同的日期格式,Java 默认的时间格式: yyyy/MM/dd HH:mm:ss
<h2>日期类型参数传递</h2>
<a href="http://localhost:8080/demo13?
id=1&birthday=2024/10/31">点我</a>
<a href="http://localhost:8080/demo13?
id=1&birthday=2024/10/31 12:12:12">点我</a>
<a href="http://localhost:8080/demo13?
id=1&birthday=2024-10-31">点我</a>
@RequestMapping("/demo13")
@ResponseBody
public String demo13(User user){
System.out.println(user);
return "success";
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
//private Date birthday;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
}
2.12 内置类型转换器
@EnableWebMvc功能:根据类型匹配对应的类型转换器
json → POJO
String → Date
默认情况下 ,SpringMVC 已经实现一些数据类型自动转换。内置转换器全都在:org.springframework.core.convert.support
java.lang.Boolean -> java.lang.String :
ObjectToStringConverter
java.lang.Character -> java.lang.Number :
CharacterToNumberFactory
java.lang.Character -> java.lang.String :
ObjectToStringConverter
java.lang.Enum -> java.lang.String :
EnumToStringConverter
java.lang.Number -> java.lang.Character :
NumberToCharacterConverter
java.lang.Number -> java.lang.Number :
NumberToNumberConverterFactory
java.lang.Number -> java.lang.String :
ObjectToStringConverter
java.lang.String -> java.lang.Boolean :
StringToBooleanConverter
java.lang.String -> java.lang.Character :
StringToCharacterConverter
java.lang.String -> java.lang.Enum :
StringToEnumConverterFactory
java.lang.String -> java.lang.Number :
StringToNumberConverterFactory
java.lang.String -> java.util.Locale :
StringToLocaleConverter
java.lang.String -> java.util.Properties :
StringToPropertiesConverter添加jackson的依赖
java.lang.String -> java.util.UUID :
StringToUUIDConverter
java.util.Locale -> java.lang.String :
ObjectToStringConverter
java.util.Properties -> java.lang.String :
PropertiesToStringConverter
java.util.UUID -> java.lang.String :
ObjectToStringConverter
三.响应
3.1 响应页面
@RequestMapping("/hello")
public String hello(){
System.out.println("Hello...");
return "success.jsp";
}
3.2 响应数据
3.2.1 具体操作
- 添加jackson的依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
- 开启自动转换json数据的支持
@Configuration
@ComponentScan({"com.lzw"})
@EnableWebMvc //开启SpringMVC JSON数据进行自动类
型转换
public class SpringMvcConfig {
}
- @ResponseBody
表明响应的是数据本身而不是一个静态资源文件,不做页面跳
转。将方法返回的对象序列化为 JSON 或 XML 格式的数据发
送给客户端。在前后端分离的项目中使用!
3.2.2 响应文本数据
<h2>响应文本数据</h2>
<a href="http://localhost:8080/demo13">点我
</a>
@RequestMapping(value = "/demo13",produces =
"text/html;charset=utf-8")
@ResponseBody
public String demo13(User user){
System.out.println(user);
return "成功";
}
3.2.3 响应JSON数据
<h2>响应JSON对象</h2>
<a href="http://localhost:8080/demo14">点我
</a>
<h2>响应JSON数组</h2>
<a href="http://localhost:8080/demo15">点我
</a>
@RequestMapping(value = "/demo14",produces =
"application/json;charset=utf-8")
@ResponseBody
public User demo14(){
User user=new User(1,"张三","123456",new
Date());
return user;
}
@RequestMapping(value = "/demo15",produces =
"application/json;charset=utf-8")
@ResponseBody
public List<User> demo15(){
List<User> list=new ArrayList<>();
User u01=new User(1,"张三","123456",new
Date());
User u02=new User(2,"李四","123456",new
Date());
list.add(u01);
list.add(u02);
return list;
}
3.3 响应统一数据格式
<h2>响应统一状态码</h2>
<input type="button" value="点我"
onclick="fn04()"/>
<script src="/js/axios-0.18.0.js"></script>
<script>
function fn04(){
axios.post("http://localhost:8080/demo16").then
(function(response){
console.log(response);
console.log(response.data);
console.log(response.data.code);
console.log(response.data.msg);
console.log(response.data.data);
});
}
</script>
@RequestMapping(value = "/demo16",produces =
"application/json;charset=utf-8")
@ResponseBody
public Result demo16(){
List<User> list=new ArrayList<>();
User u01=new User(1,"张三","123456",new
Date());
User u02=new User(2,"李四","123456",new
Date());
list.add(u01);
list.add(u02);
Result result=new Result(20000,"查询成
功",list);
return result;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private int code;//响应状态码
private String msg;//响应消息
private Object data;//响应数据
}
ok了家人们,明天见 byebye