gpt4 book ai didi

java - 时间戳 Spring 转换

转载 作者:行者123 更新时间:2023-12-01 18:59:41 25 4
gpt4 key购买 nike

我正在尝试使用时间戳。我在 jsp 中定义了一个来自 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();
}

时间戳是在插入操作中生成的,我需要它来进行删除操作。我的问题是,在 Controller 中,一旦我尝试删除最近插入的记录,此时间戳为空(但在jsp中不为空)

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

bean.timeStamp 等于 null?我确信时间戳在jsp中,所以我猜测问题出在数据转换上。

(已编辑:)我认为问题出在 initBinder 方法上,我正在做这样的事情......

@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));
}

有没有可能,日期被解析为以“dd/MM/yyyy”格式显示在JSP中,然后spring不知道如何将其再次转换为时间戳?

在 doAction 方法中,错误 var 显示此错误,看来问题出在我说的地方,但我不知道如何修复它。

"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]

最佳答案

问题写在异常中:

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

这意味着 CustonDateEditor 重新调整 java.util.Date 但您需要一个 Timestamp

因此你可以做两件事:

关于java - 时间戳 Spring 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com