Wicket:使用 CompoundPropertyModel 设置 DropDownChoice 的值

Wicket: Setting value of DropDownChoice with CompoundPropertyModel

我正在用 CompoundPropertyModel 中的数据填写表格。我的 TextAreaDateTextField 通过使用模型字段的名称作为 Id 来获取它们的值,因此它将在其父项中查找模型并通过反射找到值,如 https://ci.apache.org/projects/wicket/guide/6.x/guide/modelsforms.html。 但是我没能为我的 DropDownChoice 工作。该值保持为空。

很想知道是否有人知道我做错了什么。目前有一个解决方法,我将 FotoGroepPropertyModel 提供给我的 DropDownChoice 构造函数。

Class:

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;
    }

照片:

@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)

FotoGroep:

@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)

根据要求,我尝试覆盖 equalshashCode 但没有成功。我调试了一下,foto.fotoGroep.fotoGroepIdList<FotoGroepen>中唯一的fotoGroep.fotoGroepId是一样的。 FotoGroep 在运行时甚至是同一个 bean。我的单元测试在列表中的 FotoGroep 与在模型中的相同。

编辑,可能是错误的单元测试(?): 我正在测试 DropDownChoice 的价值:

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

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

<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>

有人可以解释这种行为吗?在我的 DropDownChoice 中使用 PropertyModel 时,它也会设置值,但在使用模型继承时不会。

我测试的 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,设置您期望的值,然后比较两者的模型。