【问题标题】:Failed to convert property value of type [java.lang.String]无法转换类型 [java.lang.String] 的属性值
【发布时间】:2017-03-22 12:57:50
【问题描述】:

我的 spring 应用程序在将“日期”保存到“数据库”时遇到问题。哪里出错了?

错误

无法将 [java.lang.String] 类型的属性值转换为 属性bornDate所需的类型[java.sql.Date];嵌套异常 是 java.lang.IllegalArgumentException:无法解析日期: 无法解析的日期:“2016-11-02”

mysql

use lifecalc;
create table Man (
manId int not null auto_increment primary key,
name varchar(30) not null,
bornDate date,
lastDate date
);
insert into man value
(null, "Pawel Cichon", "1920-11-30", "2000-02-20");

实体

@Entity
@Table(name="Man")
public class Man {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="manId")
    private int manId;

    @Column
    private String name;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column
    private java.sql.Date bornDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column
    private java.sql.Date lastDate;
    //getter end setter

控制器

@Controller
@RequestMapping("/")
public class AppController {

    @Autowired
    private ManService manService;

    @ModelAttribute("man")
    public Man modelToAddMan(){
        return new Man();
    }

    @InitBinder
     public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, true));
    }

    @RequestMapping(value="/addMan.html", method = RequestMethod.POST)
    public String addManFinish(@Valid @ModelAttribute("man") Man man, BindingResult result) {

        if(result.hasErrors()){
            return "addMan";
        } else{
            manService.addMan(man);
            return "redirect:/index.html";
        }
    }

addMan.html

<form:form method="POST" modelAttribute="man">
    <div class="form-group">
        <label for="name" class="col-sm-2 control-label">name</label>
        <form:input path="name" />
        <form:errors path="name" cssClass="error"/>
    </div>

    <div class="form-group">
        <label for="bornDate" class="col-sm-2 control-label">bornDate</label>
        <form:input   path="bornDate"  />
        <form:errors path="bornDate" cssClass="error"/>
    </div>

    <div class="form-group">
        <label for="lastDate" class="col-sm-2 control-label">lastDate</label>
        <form:input  path="lastDate"  />
        <form:errors path="lastDate" cssClass="error"/>
    </div>

    <div class="form-group">
        <input type="submit" class="btn btn-success" value="save" /> <a
            class="btn btn-danger" role="button"
            href="<spring:url value="/index.html" />">cancel </a>
    </div>
</form:form>

【问题讨论】:

  • 您从 addMan.html 输入的日期格式是 yyyy/MM/dd 还是 yyy-MM-dd?
  • "yyyy/mm/dd" 无效,应该是"yyyy/MM/dd"

标签: java mysql hibernate spring-mvc


【解决方案1】:

代替:

@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.sql.Date bornDate;

使用 java.util.date:

@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.util.date bornDate;

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 2017-05-12
    • 2018-08-21
    • 2017-04-21
    • 2021-01-23
    • 2018-10-13
    • 2017-06-14
    • 2017-05-06
    • 2015-03-29
    相关资源
    最近更新 更多