springboot-from提交时Date类型字段的正确写法

本文介绍如何在Thymeleaf模板中正确处理日期时间字段,并通过@Temporal和@DateTimeFormat注解解决Spring MVC中类型转换错误的问题。

这是一个修改数据的功能,隐藏时间字段,不作修改。

html

  <tr><td><input type="hidden" th:field="*{bloodInspectionTime}" th:value="${#dates.format(bloodResult.bloodInspectionTime, 'yyyy-MM-dd HH:mm:ss')}" ></td></tr>
        <tr><td><input type="hidden" th:field="*{bloodReportTime}" th:value="${#dates.format(bloodResult.bloodReportTime, 'yyyy-MM-dd HH:mm:ss')}" ></td></tr>

提交时报错

Field error in object 'bloodProjectResult' on field 'bloodInspectionTime': rejected value [2021-07-09 00:00:00.0]; codes [typeMismatch.bloodProjectResult.bloodInspectionTime,typeMismatch.bloodInspectionTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [bloodProjectResult.bloodInspectionTime,bloodInspectionTime]; arguments []; default message [bloodInspectionTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'bloodInspectionTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.util.Date] for value '2021-07-09 00:00:00.0'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'bloodProjectResult' on field 'bloodReportTime': rejected value [2021-07-12 00:00:00.0]; codes [typeMismatch.bloodProjectResult.bloodReportTime,typeMismatch.bloodReportTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [bloodProjectResult.bloodReportTime,bloodReportTime]; arguments []; default message [bloodReportTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'bloodReportTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.util.Date] for value '2021-07-12 00:00:00.0'; nested exception is java.lang.IllegalArgumentException]]

查了资料才知道,需要在实体类字段定义时加上@Temporal和@DateTimeFormat

@Column(name = "blood_inspection_time")
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date bloodInspectionTime;


    @Column(name = "blood_report_time")
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date bloodReportTime;

资料来源:https://stackoverflow.com/questions/20361691/formatting-thfield-in-thymeleaf

注解解析

@Temporal(TemporalType.DATE)
如果在某类中有Date类型的属性,数据库中存储可能是’yyyy-MM-dd hh:MM:ss’要获得’是’yyyy-MM-dd hh:MM:ss’,在该属性上标注 @Temporal(TemporalType.TIMESTAMP) 会得到形如’HH:MM:SS’ 格式的日期
返回到前台就是一个时间戳
结合@DateTimeFormat使用。

1.以下关于Spring Boot配置文件属性注入说法,正确的是(A)。 A、使用@ConfigurationProperties注解可以批量注入各种类型属性值 B、使用@Value注解需要逐个注入各种类型属性值 C、使用@ConfigurationProperties和@Value注解注入属性值需要设置属性Set方法 D、以上说法都错误 2.下列选项中,用于配置切点的注解是(B )。 A、@Aspect B、@Pointcut C、@Before D、@Around 3.使用MyBats框架在数据表employee中根据id删除员工信息,下列@Delete注解的写法正确的是(C)。 A、@Delete("delete from employee where id = ?") B、@Delete("delete from employee") C、@Delete("delete from employee where id = #{id}") D、以上说法都不正确 4.下列选项中对@RequestMapping注解的属性说法错误的是(C )。 A、value是@RequestMapping注解的默认属性,用于指定请求的URL B、method用于指定该方法可以处理哪种类型的请求方式 C、name用于接收指定名称的请求参数的值主要是给映射设置一个名称 D、params用于指定客户端请求中参数的值,必须包含哪些参数的值,才可以通过其标注的方法处理 5.以下有关依赖注入说法错误的是(A)。 A、依赖注入(DI)与控制反转(IoC)的含义不同,描述不同的概念 二者本质上描述的是同一个概念 B、在传统模式下,调用者通常会采用“new 被调用者”的代码方式来创建对象,这种方式会导致调用者与被调用者之间的耦合性增加,不利于后期项目的升级和维护 C、在使用Spring框架之后,控制权由应用代码转移到了Spring容器,控制权发生了反转,这就是Spring的控制反转 D、从Spring容器的角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,相当于为调用者注入了依赖的实例,这就是Spring的依赖注入 6.下列选项中,关于Spring注解的描述错误的是(B )。 A、@Autowired指定要自动装配的对象 B、@Repository指定要自动装配的对象名称 @Repository注解的主要用途是把一个类标记为数据访问层的组件 C、@Service指定一个业务逻辑组件 Bean D、@Controller指定一个控制器组件Bean 7.以下关于Spring Boot配置文件随机值设置用法正确的是(B)。 A、my.secret=#{random.value} B、my.uuid= ${random.uuid} C、my.secret=*{random.value} D、my.secret=@{random.value} 8.下列选项中,RESTful风格在HTTP请求中的约定说法错误的是(D )。 A、通过GET 、POST 、PUT和DELETE 4个动词对应四种基本请求操作 B、GET用于获取资源 C、POST用于新建资源 D、PUT用于删除资源 9.下列选项中,用于配置异常通知的注解是(D )。 A、@After B、@Around C、@AfterReturning D、@AfterThrowing 10.关于使用<foreach>元素迭代List,下列代码片段书写正确的是(B)。 A、<foreach index="index" collection="list" open="(" separator="," close=")">#{id}</foreach> B、<foreach item="id" index="index" collection="list" open="(" separator="," close=")">#{id}</foreach> C、<foreach collection="array" open="(" separator="," close=")">#{id}</foreach> D、<foreach index="index" open="("separator="," close=")">#{id}</foreach> 11. Spring的核心容器模块中提供了Spring框架的基本组成部分,包括IoC和DI功能的模块是( A)。 A、Beans B、Core C、Context D、SpEL 12.下列选项中,对于RequestMapping注解的作用说法正确的是( A)。 A、用于映射一个请求或一个方法 B、用于映射一个控制器类 C、用于映射请求参数 D、用于映射请求类型 13.下列选项中,关于@RequestMapping注解说法错误的是(D )。 A、@RequestMapping注解的默认属性是value B、@RequestMapping注解可以标注在类上和方法上 C、name属性用于映射地址指定别名 D、@RequestMapping注解的value属性必须标注 14.下列选项中关于Spring框架优点的描述错误的是( A)。 A、提供强大的、可以有效减少页面代码的标签 B、声明式事务的支持 C、方便解耦、简化开发 D、方便集成各种优秀框架 15.下列关于MyBatis中默认的常见Java类型的别名,正确的是(C)。 A、映射类型为byte,则别名为Byte B、映射类型为Byte,则别名为Byte C、映射类型为String,则别名为string D、映射类型Date,则别名为Date 16.以下有关Spring框架优点的说法不正确的是(B )。 A、Spring就大大降低了组件之间的耦合性。 B、Spring是一种侵入式框架 C、在Spring中,可以直接通过Spring配置文件管理数据库事务,省去了手动编程的繁琐,提高了开发效率。 D、Spring对Java EE开发中的一些API(如JDBC、JavaMail等)都进行了封装,大大降低了这些API的使用难度。 17. 下列选项中,声明当前属性对应数据表的主键的注解是(C)。 A、@Entity B、@Table C、@Id D、@Column 18.下列选项中,使用MyBatis-Plus,可以指定实体类中属性对应字段名的注解是(D)。 A、@Entity B、@TableName C、@TableId D、@TableField 19.在MyBatis配置文件中,下列关于<mapper>元素的说法正确的是(C)。 A、<mapper>元素的namespace属性是不唯一的 B、<mapper>元素的namespace属性值的命名不一定跟接口同名 C、<mapper>元素不是映射文件的根元素 D、<mapper>元素是映射文件的根元素 20.下面关于映射文件中的<mapper>元素的属性,说法正确的是(D)。 A、parameterType属性的值表示的是返回的实体类对象 B、namespace属性的值通常设置为对应实体类的全限定类名 C、resultType属性的值表示传入的参数类型 D、以上说法都不正确 21.以下关于MyBatis的<set>元素的使用及说法正确的是(A)。 A、<set>元素主要用于更新操作,其主要作用是在动态包含的SQL语句前输出一个SET关键字,并将SQL语句中最后一个多余的逗号去除 B、使用MyBatis的<set>元素来更新操作,前端需要传入所有参数字段,否则未传入字段会默认设置为空 C、在映射文件中使用<set>和<if>元素组合进行update语句动态SQL组装, <set>元素内包含的内容可以都为空,<if>元素会进行判断处理 D、在映射文件进行更新操作,只需要使用<set>元素就可以进行动态SQL组装 22.要求根据员工的id查找员工信息,下列使用@Select注解的代码书写正确的是(D)。 A、@Select("select * from tb_worker where id = ?") B、@Select("select * from tb_worker where id = #id") C、@Select("select * from tb_worker where id = {id}") D、@Select("select * from tb_worker where id = #{id}") 23.在Spring框架中实现控制反转的是(B)容器 A、SpringMVC B.SpringIOC C.SpringAOP D.Spring Bean 24.在SpringMVC中,需要使用(A)注解将请求与处理方法一一对应 A.@RequestMapping B.@Controller C.@RequestBody D.@ResponseBody 25.以下关于Spring Boot多环境配置文件名格式,正确的是(A)。 A、application-dev.properties B、application.test.properties C、application.prod.yaml D、application_prod.yml 26.下面关于Java对象之间的关联关系描述正确的是(B)。 A、一对一的关系就是在本类和对方类中定义同一个类型的对象 B、一对多的关系:就是一个A类类型对应多个B类类型的情况 C、多对多的关系只需要在一方的类中引入另一方类型的集合 D、多对多关联关系需要在本类中引入本类的集合 27.下列选项中,对于创建Maven项目,界面中Group和Artifact的作用说法错误的是(C)。 A、Group为项目的组名,通常设置为公司或组织的反向域名 B、Artifact为项目的名称,Maven管理项目包用作区分的字段 C、Artifact为所创建项目在本地存放的路径 D、Group和Artifact是Maven区分项目包的字段 28.下列选项中,不属于@SpringBootApplication注解内部包含的注解的是(D)。 A、@SpringBootConfiguration B、@EnableAutoConfiguration C、@ComponentScan D、@Configuration 29.使用<if>元素判断username字段是否为空,下列代码的书写正确的是(C)。 A、<if test="username !=' ' ">username=#{username}, </if> B、<if test="username !=null and username !=' '"> username=#{username}</if> C、<if test="username !=null and username !=' '"> username=#{username},</if> D、<if test="username !=null">username=#{username},</if> 30.关于Spring Boot项目的打包部署相关说法错误的是(C) A、使用IDEA快速方式创建的项目会自动导入项目打包插件 B、Spring Boot项目默认以Jar包方式打包 C、Spring Boot默认支持内嵌式Tomcat,在不添加任何依赖创建Spring Boot项目,也会提供内嵌tomcat D、可以通过IDEA工具进行项目快速打包和部署
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值