java - 使用动态实体属性循环文本字段组件

标签 java reflection tapestry

我是 Tapestry5 用户,我有一个表单,上面有无数字段,所有字段都以数字递增。

示例

时间表.tsHours01 时间表.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 以保持其完全动态。

最佳答案

首先,您可以使用 PropertyAccess 服务作为获取动态计算属性的更简单的方法。

什么是设置和更改id? TsWhour 是一个组件吗?

基本上,在表单提交时,Tapestry 需要回溯其步骤到每个组件以及该 id 属性的每次更改。表单内的循环组件知道如何执行此操作(尽管您可以对其进行配置以进行一些优化)。

您没有提供足够的背景信息。

最后,如果您有“无数”几乎相同的字段,那么您的数据建模可能存在问题。也许您需要的看起来更像是映射而不是对象。也许您需要一种简单的方法来将数据从实体类型(我假设您正在使用 Hibernate 或 JPA)提取到更易于在 UI 层中使用的中间类型。

关于java - 使用动态实体属性循环文本字段组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18857921/

相关文章:

Tapestry 单选按钮事件?

java - AES 解密的有效结果在一个字节和一个字节中不同的可能性有多大?

java - 如何在 Android 中链接 GPS 追踪器?

c# - 通过反射找到实现通用接口(interface)的类型

c# - 使用反射在异步方法中获取方法名称不会返回预期结果

grid - Tapestry:如何在自定义网格标题中保留排序功能?

java - 二叉树 - 后序

java - 正则表达式查找不完整的 Java 断言

c# - 表达式.Convert : Object of type 'System.Int64' cannot be converted to type 'System.Int32'

java - 如何使用来自网络服务的列表填充 Tapestry5 调色板?