Wicket DropDownChoice NOT working correctly with PropertyModels

旧城冷巷雨未停 提交于 2019-12-13 14:03:42

问题


I have been trying to debug why my DropDownChoice in a simple form with just the DropDown and a Submit Button hasn't been working correctly for hours now.

It has a very weird behaviour. Where the first value selected in the drop down choice is sent successfully to the server after which any other choice select is not updated by the model. ie if I have a List persons, and I select the 2nd person, it submits this successfully. However, on select of another person and trying to submit it again it keeps showing the first selected option.

Snippets of code here:

 ChoiceRenderer<Empowerment> empowermentChoiceRenderer = new ChoiceRenderer<>("name", "id");
 final DropDownChoice<Empowerment> empowermentDropDownChoice =
                    new DropDownChoice<>("empowerment", new PropertyModel<Empowerment>(this, "empowerment"), empowermentList, empowermentChoiceRenderer);
 empowermentDropDownChoice.setRequired(true);
 add(empowermentDropDownChoice);

Only way I am able to get a decent behaviour is if I set the empowerment variable above to null. In this case, on submit the empowerment is reinitialised to null and then a new submit works correctly.

empowerment is just a JPA entity.

I'll be glad to know if this is a known issue. I experienced it in wicket 6.9.1 and wicket 6.12


回答1:


Finally, found the solution to the problem. Above code is correct, but problem lied in the entity class itself - Empowerment needs to implement Equals and Hashcode correctly.

The DropDownChoice works just fine after this.




回答2:


Add an OnChangeAjaxBehavior to your DropDownChoice. This will update the model value on every selection change you make on the drop down:

empowermentDropDownChoice .add(new OnChangeAjaxBehavior() {

    @Override
    protected void onUpdate(AjaxRequestTarget art) {
        //just model update
    }
});


来源:https://stackoverflow.com/questions/20282781/wicket-dropdownchoice-not-working-correctly-with-propertymodels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!