Spring – Error: org.springframework.core.convert.ConversionFailedException. How fix it

hibernatejettyspring

I have a problem with my project. When i sumbmit UserForm to save a User then occur a following error:

Failed to convert property value of type java.lang.String to required
type java.util.Date for property birthDay; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type java.lang.String to type java.util.Date for value
2016-03-26; nested exception is java.lang.IllegalArgumentException

Here file UserForm.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>         
<html>
    <head>
        <title>Add New User</title>
    </head>
<body>
<h2>Please Input User Information</h2>

    <form:form method="POST" modelAttribute="User" action="/user/save">
        <table>
            <tr>
                <td><form:label path="userName">UserName</form:label></td>
                <td><form:input path="userName"/></td>
                <td><form:errors path="userName" /></td>
            </tr>
            <tr>
                <td><form:label path="passWord">Password</form:label></td>
                <td><form:input path="passWord"/></td>
                <td><form:errors path="passWord" /></td>
            </tr>


            <tr>
                <td><form:label path="birthDay">Birthday</form:label></td>
                <td><form:input path="birthDay" type = "date"/> </td>

                <td><form:errors path="birthDay"/> </td>
            </tr>

            <tr>
                <td><form:label path="age">Age</form:label>
                <td><form:input path="age" type = "number"/> </td>
                <td><form:errors path="age"></form:errors> </td>
            </tr>

            <tr> 
                <td><form:label path="gender">Gender</form:label></td>
                <td><form:select path="gender">
                        <form:options items="${allgender}" itemLabel="gender"/>

                    </form:select></td>
                <td><form:errors path="gender"></form:errors></td>
            </tr>





            <tr><td colspan="3"><input type="submit" value="Submit" /></td></tr>
        </table>
    </form:form> 

</body> 
</html>

Here file User.java

package edu.java.spring.service.user.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
//@Table(name = "user",uniqueConstraints={@UniqueConstraint(columnNames="username")})
public class User {
//  @Column(name = "gender", nullable = false)
//  @Enumerated(EnumType.STRING)
    public Gender getGender() {
        return gender;
    }
    public void setGender(Gender gender) {
        this.gender = gender;
    }
//  @Id
//  @GeneratedValue(strategy = GenerationType.IDENTITY)
//  @Column(name = "username", unique = true, nullable = false)
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
//  @Column(name = "password", nullable = false)
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
//  @Column(name = "birthday", nullable = false)
    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }
//  @Column(name="age", nullable = false)
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    private String userName;
    private String passWord;
    private Date birthDay;
    private Integer age;
    private Gender gender;


}

Best Answer

Spring cannot convert a request parameter string into an object that contains the date (and time) information to java.util.Date object.

Becaue Spring doesn't know how to convert your date string to Date object because date format may vary depending on the locale

Spring has CustomDateEditor to convert this for you. You just need to register your date format.

You should register your Date formats using @InitBinder

 @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yourdateformat"); //yyyy-MM-dd'T'HH:mm:ssZ example
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
Related Topic