Wicket:使用CompoundPropertyModel设置DropDownChoice的值

时间:2017-11-14 11:24:08

标签: java reflection wicket

我正在使用CompoundPropertyModel的数据填写表单。我的TextAreaDateTextField通过使用模型字段的名称作为Id获取其值,因此它将在其父项中查找模型,并通过反射查找值,如{{ 3}}。 但我没有让这个工作在DropDownChoice。该值保持为空。

如果有人知道我做错了什么,我会很乐意听到。目前有一个workarround,我将PropertyModel的{​​{1}}提供给我的FotoGroep构造函数。

类别:

DropDownChoice

图片:

public class ImageControlForm<T extends Foto> extends StatelessForm<Foto> {

    private TextArea<String> beschrijving;
    private DateTextField datum;
    private DropDownChoice<FotoGroep> groep;

    public ImageControlForm(String id, CompoundPropertyModel<Foto> fotoModel) {
        super(id, fotoModel);

        setMultiPart(true);
        setDefaultModel(fotoModel);

        add(maakBeschrijvingField());
        add(maakDatumField());
        add(maakGroepField());
    }

    private TextArea<?> maakBeschrijvingField() {
        beschrijving = new TextArea<>("beschrijving");
        return beschrijving;
    }

    private DateTextField maakDatumField() {
        datum = new DateTextField("datum", "d/M/yy");
        datum.add(new DatumPicker());
        return datum;
    }

    private DropDownChoice<FotoGroep> maakGroepField() {
        Order sortOrder = Helper.createOrder("naam", SortOrder.ASC);
        List<FotoGroep> fotoGroepen = databaseService.getPictureGroups(sortOrder);
        groep = new DropDownChoice<>("fotoGroep", fotoGroepen, new ChoiceRenderer<FotoGroep>("naam", "fotoGroepId"));
        groep.isRequired();
        return groep;
    }

FotoGroep:

@Entity
@Table(name = "XS_FOTO")
public class Foto extends BasisModel implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "FOTO_ID")
    private Long fotoId;

    @Column(name = "BESCHRIJVING", nullable = true)
    private String beschrijving;

    @Column(name = "DATUM", nullable = true)
    @Temporal(TemporalType.DATE)
    private Date datum;

    @ManyToOne
    @JoinColumn(name = "FOTO_GROEP_ID", nullable = false)
    private FotoGroep fotoGroep = new FotoGroep(Long.valueOf(12));

    (getters and setters)

根据要求,我尝试覆盖@Entity @Table(name = "XS_FOTO_GROEP") public class FotoGroep extends BasisModel implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "FOTO_GROEP_ID") private Long fotoGroepId; @Column(name = "NAAM", nullable = false) private String naam; @Override public boolean equals(Object object) { return (this.getFotoGroepId().equals(((FotoGroep)object).getFotoGroepId())); } @Override public int hashCode() { return Objects.hash(fotoGroepId, naam, beschrijving, datum); } (getters and setters) equals,但没有成功。我调试了hashCodefoto.fotoGroep.fotoGroepId中唯一的fotoGroep.fotoGroepId相同。 List<FotoGroepen>两者在运行时都是相同的bean。我的单元测试在列表中与模型中的FotoGroep相同。

编辑,可能是坏单元测试(?): 我正在测试FotoGroep的值:

DropDownChoice

值保持返回Null。 但是当我检查HTML时,我可以看到选择了正确的选项:

assertEquals("456", formTester.getTextComponentValue("fotoGroep"));

有人可以解释这种行为吗?在我的<select wicket:id="fotoGroep" name="fotoGroep" disabled="disabled"> <option value="123">naam</option> <option value="123">naam</option> <option value="123">naam</option> <option value="123">naam</option> <option value="123">naam</option> <option value="123">naam</option> <option selected="selected" value="456">naam</option> </select> 中使用PropertyModel时,它也设置了值,但在使用模型继承时却没有。

1 个答案:

答案 0 :(得分:3)

我正在测试DropDownChoice错误。

我正在测试DropDownChoice的值:

assertEquals("456", formTester.getTextComponentValue("fotoGroep"));

但在我的情况下,此值从未设置,但是选择了正确的选项!我已将测试改为:

DropDownChoice<FotoGroep> dropDownChoice = (DropDownChoice)tester.getComponentFromLastRenderedPage("imageControlPanel:imageControlForm:fotoGroep");
dropDownChoice.setModelObject(createFotoGroep);
tester.assertModelValue("imageControlPanel:imageControlForm:fotoGroep", dropDownChoice.getModelObject());

测试将采用DropDownChoice,设置您期望的值,然后比较两者的模型。