elcadro
elcadro

Reputation: 1492

Timestamp Spring conversion

I'm trying to work with timestamp. I've defined in the jsp a hidden var from a bean.

<form:input type="hidden" path="timeStamp" />

private Timestamp timeStamp;

public final Timestamp getTimeStamp() {
return (timeStamp == null)
? null : (Timestamp) timeStamp.clone();
}

public final void setTimeStamp(Timestamp timeStamp) {
this.timeStamp = (timeStamp == null)
? null : (Timestamp) timeStamp.clone();
}

The Timestamp is generated in the insert operation, and I need it for the delete op. My problem is, that in the controller, once I'm trying to delete the record recently inserted, this timeStamp is null (but it isn't null in the jsp)

public final void doActionDelete(DumyBean bean, Errors errors, ActionRequest actionrequest...)

bean.timeStamp is equal to null?? I'm sure that the timestamp is in the jsp, so I guess the problem is about data conversion.

(Edited:) I think that the problem is the initBinder method, where I'm doing something like this...

@InitBinder
public final void initBinder(WebDataBinder binder) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.setLenient(false);

    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

Is it possible that, the date is parsed to be shown in the JSP with the "dd/MM/yyyy" format, and after that spring doesn't know how to transform it again into timeStamp??

In the doAction method, the errors var show this error, that seems the problem is where I said, but I've no idea how to fix it.

"Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'timeStamp'; 
nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type
[java.sql.Timestamp] for property 'timeStamp': PropertyEditor [org.springframework.beans.propertyeditors.CustomDateEditor] 
returned inappropriate value of type [java.util.Date]

Upvotes: 0

Views: 10052

Answers (1)

Ralph
Ralph

Reputation: 120811

The problem is written in the exception:

PropertyEditor [org.springframework.beans.propertyeditors.CustomDateEditor] returned inappropriate value of type [java.util.Date]

This mean the CustonDateEditor retuns a java.util.Date but you need a Timestamp.

Therefore you can do two things:

Upvotes: 4

Related Questions