我是 Tapestry5 用户,我有一个表格,上面有无数个字段,所有字段都以数字递增。
例子
timesheet.tsHours01 timesheet.tsHours02 等
我想动态循环它,但未能完全让它工作。到目前为止我所尝试的。
@Property
private List<TimeSheetEntity> timeSheetEntity;
@Property
private List<TsWhour> tsWhours;
@Property
private TsWhour tsWhour;
public void onPrepareFromForm() {
this.timeSheetEntity = timeSheetService.getCreateOrUpdate(timeSheetEntity);
tsWhours = new ArrayList<TsWhour>();
tsWhours.add(new TsWhour(timeSheetEntity, "TsWhours01"));
tsWhours.add(new TsWhour(timeSheetEntity, "TsWhours02"));
tsWhours.add(new TsWhour(timeSheetEntity, "TsWhours03"));
}
public class TsWhour {
private TimeSheetEntity timeSheetEntity;
private String id;
private BigDecimal property;
public TsWhour() {
}
public TsWhour(TimeSheetEntity timeSheetEntity, String id) {
this.timeSheetEntity = timeSheetEntity;
this.id = id;
}
public BigDecimal getProperty() {
try {
Method method = TimeSheetEntity.class.getMethod("get" + id);
return (BigDecimal) method.invoke(timeSheetEntity);
} catch (NoSuchMethodException ex) {
Logger.getLogger(TimeSheet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(TimeSheet.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(TimeSheet.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(TimeSheet.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(TimeSheet.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public void setProperty(BigDecimal value) {
System.out.println(value);
}
public TimeSheetEntity getTimeSheetEntity() {
return timeSheetEntity;
}
public void setTimeSheetEntity(TimeSheetEntity timeSheetEntity) {
this.timeSheetEntity = timeSheetEntity;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我能够加载页面,但是当我保存它时失败。如果可能的话,我宁愿不将数据类型指定为 bigdecimal 以保持它完全动态。