🍁博客主页:👉 不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:👉 SpringMVC注解开发
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
🔥欢迎大佬指正,一起 学习!一起加油!
日期型参数传递
- 日期类型数据基于系统不同格式也不尽相同
- 2088-08-18
- 2088/08/18
- 08/18/2088
- 接收形参时,根据不同的日期格式设置不同的接收方式
@DateTimeFormat
- 类型:形参注解
- 位置:SpringMVC控制器方法形参前面
- 作用:设定日期时间型数据格式
- 属性:pattern:日期时间格式字符串
//7.日期参数 @DateTimeFormat 该注解自动会解析处理,会把字符串类型
@RequestMapping("/date")
@ResponseBody
public String dataParam(Date date,@DateTimeFormat(pattern="yyyy-MM-dd") Date date1,@DateTimeFormat(pattern="yyyy-MM-dd hh:mm:ss") Date date2){
System.out.println("参数传递data:"+date);
System.out.println("参数传递data1:"+date1);
System.out.println("参数传递data2:"+date2);
return "'module':'data.....'";
}
响应json数据
之前:
@ResponseBody
- 类型:方法注解
- 位置: SpringMIVC控制器方法定义上方
- 作用:设置当前控制器方法响应内容为当前返回值,无需解析
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("Book save....");
return "'module':'Book Save'";
}
}
而现在:
@ResponseBody
- 类型:方法注解
- 位置: SpringMVC控制器方法定义上方
- 作用:设置当前控制器返回值作为响应体
@RequestMapping("/text")
@ResponseBody
public String text(){
System.out.println("响应文本数据");
return "response text";
}
案例分析:
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringMVC-Demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</project>
2.ServletContainersInitConfig
package com.study.config;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
/*
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//加载SpringMVC容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
//设置哪些请求可以归属SpringMVC处理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
//加载Spring容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
}*/
3.SpringMvcConfig
package com.study.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.study.controller")
public class SpringMvcConfig {
}
4.UserController
package com.study.controller;
import com.study.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.List;
@Controller
public class UserController {
//页面跳转
@RequestMapping("/jump")
public String show(){
System.out.println("页面跳转");
return "jump.jsp";
}
//响应文本数据
@RequestMapping("/text")
@ResponseBody
public String text(){
System.out.println("响应文本数据");
return "response text";
}
//响应pojo对象
@RequestMapping("/jsonPojo")
@ResponseBody
public User jsonPojo(){
System.out.println("返回json对象数据");
User user = new User();
user.setName("小马哥");
user.setAge(34);
Address add=new Address();
add.setProvince("河南");
add.setCity("商丘");
user.setAddress(add);
return user;
//{"name":"小马哥","age":34,"address":{"province":"河南","city":"商丘"}}
}
//响应pojo集合对象
@RequestMapping("/jsonList")
@ResponseBody
public List<User> jsonList(){
User user1 = new User();
user1.setName("小飞侠");
user1.setAge(45);
Address add=new Address();
add.setProvince("河南");
add.setCity("商丘");
user1.setAddress(add);
User user2 = new User();
user2.setName("罗西");
user2.setAge(45);
Address add1=new Address();
add1.setProvince("河南");
add1.setCity("郑州");
user2.setAddress(add1);
List<User> list=new ArrayList<>();
list.add(user1);
list.add(user2);
return list;
//[{"name":"小飞侠","age":45,"address":{"province":"河南","city":"商丘"}},{"name":"罗西","age":45,"address":{"province":"河南","city":"郑州"}}]
}
}
5.Address
package com.study.domain;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
6.User
package com.study.domain;
public class User {
private String name;
private int age;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}