【发布时间】:2014-10-08 14:51:53
【问题描述】:
我正在使用 wicket 和 hibernate 来开发网站。我只是想保存表单的一些数据。 我的对象是这样的:
@Entity
@Table(name = "PRODUCT")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DISCRIMINATOR",discriminatorType=DiscriminatorType.STRING )
@DiscriminatorValue(value="product")
public class Product implements Serializable {
@Id
@GeneratedValue
@Column(name = "PRODUCT_ID")
private int productID;
@Column(name = "START_DATE")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime startDate;
public DateTime getStartDate() {
return startDate;
}
public void setStartDate(DateTime startDate) {
this.startDate = startDate;
}
}
我得到了一个表单,该表单内部有几个输入字段。对于我使用的日期字段:
DatePicker datePicker = new DatePicker();
DateConverter dateConverter = new PatternDateConverter ( "dd.MM.yyyy", false );
final DateTextField startDate = new DateTextField("startDate",dateConverter);
startDate.add(datePicker);
如果我提交表单,我会收到此异常:
Last cause: Could not convert value: 08.10.14 to type: org.joda.time.DateTime. Could not find compatible converter.
WicketMessage: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at [Form [Component id = form]] on component [Form [Component id = form]] threw an exception
我猜是因为 wicket 使用 setter 作为 startDate。但是我该如何编辑呢?
【问题讨论】:
-
您的表单是否使用
CompoundPropertyModel?如果为真,那么我认为它会尝试将Date转换为DateTime,但它失败了。尝试为您的DateTextField(例如new PropertyModel<Date>(this,"customDate"))使用自定义模型以使用Date对象存储实际值,并在提交表单时-> 将customDate转换为DateTime对象并设置为Product。 -
在存储
DateTextField时,我使用@Temporal(TemporalType.TIMESTAMP)而不是@Type,然后我使用Date对象private Date startDate;。也可以将TemporalType设置为Date -
嘿,我想我意识到我还没有完全理解模型。正如@Michael 所说,我在 DateTextField 中添加了一个日期模型。
标签: java forms hibernate wicket