java Wicket:从表单设置JodDateTime
我正在使用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用二传来开始。但是我如何编辑这个
# 1 楼答案
如果对表单使用
CompoundPropertyModel
,则表单组件将搜索实际模型对象的相应字段,以将值应用于(wicket:form组件的id必须等于模型字段的名称,并且此字段的类型必须与表单组件返回的值相同)但如果模型中没有相应的字段,或者我们想要使用独立的表单组件,那么我们只需要为此类组件使用另一个模型
出现异常是因为
DateTextField
在转换后返回Date
,并且您的模型有DateTime
字段所以,你至少有两种方法来解决这个问题:
onSubmit
方法的表单组件获取的Date
值转换为DateTime
对象李>DateTextField
为模型返回DateTime
对象。查看here或here(参见src中的URLTextField
),以更好地了解如何实现这一点。您可以依靠org.apache.wicket.datetime.DateConverter
src实现自己的转换器李>