我正在使用 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 使用 startDate 的 setter 。但我该如何编辑它呢?
最佳答案
如果您的表单使用CompoundPropertyModel
,那么表单组件将搜索真实模型对象的相应字段,并将值应用到(表单组件的wicket:id必须等于模型字段的名称,并且该字段的类型必须与表单组件返回的类型相同)。
但是如果模型中没有对应的字段,或者我们想使用独立的表单组件,那么我们只需要为该组件使用另一个模型即可。
出现异常的原因是 DateTextField
在转换后返回 Date
并且您的模型具有 DateTime
字段。
因此,您至少有两种方法来处理这个问题:
关于java - Wicket:从表单设置 JodaDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26259963/