springmvc--对日期类型如何处理
目录
一、Student类:
二、student.jsp(在webapp下)
三、StudentController类
四、启动tomcat
1.”/”形式
2.“-”形式
3.“\”形式
五、解决
1.第一种方式@DateTimeFormat注解
(1)在实体类的birthday上加@DateTimeFormat注解
(2)同时在spring-mvc.xml里配置spring开启注解mvc的支持
(3)重新启动tomcat
2.第二种方式:用自定义类型转换器
(1)先把Studeunt类birthday上的注解去掉
(2)建一个StringToDate类:
(3)spring-mvc.xml里配置日期类型转换器
(4)重新启动tomcat:
六、怎么让控制台输出的是自己输入的格式?
1.最简单的方式:在输出语句那用SimpleDateFormat对象
StudentController类里:
运行:
控制台:
2.实体类的日期上加两个注解(@DateTimeFormat和@JsonFormat)
(1)使用@JsonFormat注解先导入依赖
(2)实体类:
(3)spring-mvc.xml里配置spring开启注解mvc的支持
一、Student类:
import java.util.Date;
public class Student {
private String stuName;
private Integer age;
private Date birthday;
public Student() {
}
public Student(String stuName, Integer age, Date birthday) {
this.stuName = stuName;
this.age = age;
this.birthday = birthday;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"stuName='" + stuName + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
二、student.jsp(在webapp下)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="stu/student.do" method="post">
姓名:<input type="text" name="stuName" /><br/>
年龄:<input type="text" name="age" /><br/>
生日:<input type="text" name="birthday" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
三、StudentController类
import com.qcby.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/stu")
public class StudentController {
@RequestMapping("/student.do")
public String studentSave(Student student){
System.out.println(student);
return "success";
}
}
四、启动tomcat
1.”/”形式
2.“-”形式
3.“\”形式
五、解决
1.第一种方式@DateTimeFormat注解
(1)在实体类的birthday上加@DateTimeFormat注解
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class Student {
private String stuName;
private Integer age;
//这是"-"形式的,想要别的形式就去改
@DateTimeFormat(pattern = "yyyy-MM-dd") //这里还可以加上时分秒:yyyy-MM-dd HH:mm:ss
private Date birthday;
public Student() {
}
public Student(String stuName, Integer age, Date birthday) {
this.stuName = stuName;
this.age = age;
this.birthday = birthday;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"stuName='" + stuName + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
(2)同时在spring-mvc.xml里配置spring开启注解mvc的支持
<!-- 配置spring开启注解mvc的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
(3)重新启动tomcat
2.第二种方式:用自定义类型转换器
(1)先把Studeunt类birthday上的注解去掉
(2)建一个StringToDate类:
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate implements Converter<String, Date> {
@Override
public Date convert(String s) {
//判断
if(s==null){
throw new RuntimeException("请您输入日期");
}
//进行转换
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
//进行转换
return simpleDateFormat.parse(s);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
(3)spring-mvc.xml里配置日期类型转换器
但是这是自己写的,框架不认识,所以要在spring-mvc.xml里配置:
<!--配置日期类型转换器,类型转换器的组件,把日期类型转换注入到组件对象中-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.qcby.controller.StringToDate"></bean>
</set>
</property>
</bean>
<!--让映射器、适配器和处理器生效(默认不配置也是可以的)-->
<mvc:annotation-driven conversion-service="conversionService"/><!--这个要开启-->
<!-- 配置spring开启注解mvc的支持-->
<!--<mvc:annotation-driven></mvc:annotation-driven>--><!--而且这个要关闭-->
(4)重新启动tomcat:
六、怎么让控制台输出的是自己输入的格式?
1.最简单的方式:在输出语句那用SimpleDateFormat对象
StudentController类里:
import com.qcby.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.text.SimpleDateFormat;
@Controller
@RequestMapping("/stu")
public class StudentController {
@RequestMapping("/student.do")
public String studentSave(Student student){
System.out.println(student);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(student.getBirthday());
System.out.println(format);
return "success";
}
}
运行:
控制台:
2.实体类的日期上加两个注解(@DateTimeFormat和@JsonFormat)
这样就是接收的时候就以这种方式接收,显示的时候还以这种方式显示,就不用自定义类型转换器了
(1)使用@JsonFormat注解先导入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
(2)实体类:
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class Student {
private String stuName;
private Integer age;
@DateTimeFormat(pattern = "yyyy-MM-dd") //输入
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8") //显示
private Date birthday;
public Student() {
}
public Student(String stuName, Integer age, Date birthday) {
this.stuName = stuName;
this.age = age;
this.birthday = birthday;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"stuName='" + stuName + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
(3)spring-mvc.xml里配置spring开启注解mvc的支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置spring创建容器时要扫描的包 -->
<context:component-scan base-package="com.qcby.controller"></context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置spring开启注解mvc的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>