首页 > 编程语言 > 详细

Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-06'; nested exception is java.lang.IllegalArgumentException]解决

时间:2020-02-09 17:50:19      阅读:209      评论:0      收藏:0      [点我收藏+]

今天做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]解决

原文:https://www.cnblogs.com/jpxjx/p/12287613.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!