zoukankan      html  css  js  c++  java
  • Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-06'; nested exception is java.lang.IllegalArgumentException]解决

    今天做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;包下的
    学习没有一蹴而就,放下急躁,一步一步扎实前进
  • 相关阅读:
    torchline:让Pytorch使用的更加顺滑
    论文笔记系列-AutoFPN
    Latex: 添加IEEE会议论文作者信息
    Latex citation using natbib and footnotesize
    解决 Boost安装:fatal error: bzlib.h: No such file or directory 问题
    将 Graphviz .dot 文件转换为其他格式的图像
    Mac环境下扩容 .vmdk 镜像容量
    解决 dpkg: warning: files list file for package 'x' missing 问题
    Latex 左右引号
    Latex 三线表及设置列数
  • 原文地址:https://www.cnblogs.com/jpxjx/p/12287613.html
Copyright © 2011-2022 走看看