今天做springbook项目前端输入日期传到数据库保存报了一下错误

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Feb 09 16:31:32 CST 2020
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='book'. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'book' on field 'bookDate': rejected value [2020-02-06]; codes [typeMismatch.book.bookDate,typeMismatch.bookDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [book.bookDate,bookDate]; arguments []; default message [bookDate]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-06'; nested exception is java.lang.IllegalArgumentException] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.constructAttribute(ModelAttributeMethodProcessor.java:317) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:224) at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:127) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)。。。
前端代码:
<form th:action="@{/toAdd}">
<h2>添加图书信息</h2>
书名:<input type="text" name="bookName"/><br/>
简介:<input type="text" name="bookNote"/><br/>
作者:<input type="text" name="bookAuthor"/><br/>
价格:<input type="text" name="bookMoney"/><br/>
出版日期:<input type="date" name="bookDate"/><br/>
<input type="submit" value="添加"/>
</form>

数据库:

百度上说的方法差不多,就是在对应controller中添加一下代码
 @InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

我试了一下果然可以!

代码如下:

@RequestMapping("/toAdd")
public String toAdd(Book book,Model model) { bookService.addBook(book);
return "redirect:/toQueryAll"; }
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

注意这里的

Date.class是
import java.util.Date;包下的

Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-06'; nested exception is java.lang.IllegalArgumentException]解决的更多相关文章

  1. 【spring boot】spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]

    spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [jav ...

  2. Spring 整合 Flex (BlazeDS)无法从as对象 到 Java对象转换的异常:org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Date' to required type 'java.sql.Timestamp' for property 'wfsj'; nested exception is java.lang.Ill

    异常信息如下: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value ...

  3. No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, org.springframework.boot.logging.LogLevel>]

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  4. No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  5. 异常:Instantiation of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.base.Preconditions.che ckState(ZLjava/lang/String;I)V

    Instantiation of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.bas ...

  6. Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.codec.digest.DigestUtils.sha1Hex(Ljava/lang/String;)Ljava/lang/String;

    异常:Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.co ...

  7. Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type

    问题描述 将项目挂载到 Myeclipse 的 tomcat 上,启动 tomcat ,报错“Initialization of bean failed; nested exception is ja ...

  8. nested exception is java.lang.IllegalArgumentException: warning no match for this type name: res [Xlint:invalidAbsoluteTypeName]

    注:内有单词(sping)写错,请忽略,不影响程序运行 运行时报错: Exception in thread "main" org.springframework.beans.fa ...

  9. Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config解决

    出现这个问题往往伴随  HTTP-500错误 报错信息: HTTP Status - Handler processing failed; nested exception is java.lang. ...

随机推荐

  1. WebGPU 导入[2] - 核心概念与重要机制解读

    目录 1. 核心概念 ① 适配器和设备 ② 缓冲.纹理.采样器 ③ 绑定组 ④ 着色器与管线 ⑤ 编码器与队列 2. 重要机制 ① 缓冲映射机制 ② 时间线 1. 核心概念 这部分不会详细展开,以后写 ...

  2. 5.8 NOI 模拟

    \(T1\) 比较容易想到的 二分转化为判定,判定是否存在一个子图能保证能一直在\(x\)时间内到达\(n\) 设\(dis(u,v)\)表示\(u->v\)的最短路 先找出候选节点\(i,di ...

  3. 技术分享 | load data导致主键丢失的神秘问题

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 1 ...

  4. layui框架图片上传至服务器

    注意:只可用于数据量较小的项目,数据量庞大的项目不要用这个,否则会造成图片数量庞大,至服务器运行速度变慢或瘫痪 HTML代码 //前端使用的是layui框架<div class="la ...

  5. Docker 08 部署Elasticsearch

    参考源 https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from=333.999.0.0 https://www.bilibili.com/vid ...

  6. React报错之Type '() => JSX.Element[]' is not assignable to type FunctionComponent

    正文从这开始~ 总览 当我们尝试从函数组件中返回元素组成的数组时,会产生"Type '() => JSX.Element[]' is not assignable to type Fu ...

  7. 【ARK UI】HarmonyOS ETS 资源管理基本使用

    ​代码实现 api讲解 getStringArray getStringArray(resId: number, callback: AsyncCallback<Array>): void ...

  8. 如何结合整洁架构和MVP模式提升前端开发体验 - 整体架构篇

    本文不详细介绍什么是整洁架构以及 MVP 模式,自行查看文章结尾相关链接文章. 整洁架构粗略介绍 下图为整洁架构最原始的结构图: Entities/Models:实体层,官方说法就是封装了企业里最通用 ...

  9. 第九十二篇:Vue 自定义指令

    好家伙, 1.什么是自定义指令? vue官方提供了v-text,v-for,v-model,v-if等常用的指令.除此之外vue还允许开发者自定义指令. 2.自定义指令的分类 vue中的自定义指令分为 ...

  10. 第五十五篇:Axios的封装

    好家伙, 上图 1.为什么需要封装axios? 当我们改变项目的使用环境时候,url也会随之改变,那么我们就需要改很多axios请求中的url配置 现在我们将axios封装,在项目使用环境改变时我们只 ...