我是Wicket的新手,但谷歌搜索这个问题并没有给我任何有意义的东西.所以我希望有人可以提供帮助.
我有一个扩展Form的SiteChoice对象,以及一个扩展DropDownChoice的SiteList对象.我的SiteChoice类看起来像:
public class SiteChoice extends Form {
public SiteChoice(String id) {
super(id);
addSiteDropDown();
}
private void addSiteDropDown() {
ArrayList<DomainObj> siteList = new ArrayList<DomainObj>();
// add objects to siteList
ChoiceRenderer choiceRenderer = new ChoiceRenderer<DomainObj>("name", "URL");
this.add(new SiteList("siteid",siteList,choiceRenderer));
}
}
Run Code Online (Sandbox Code Playgroud)
然后我只是将我的SiteChoice对象添加到我的Page对象中:
SiteChoice form = new SiteChoice("testform");
add(form);
Run Code Online (Sandbox Code Playgroud)
我的Wicket模板有:
当我调出页面时,它呈现正常 - 下拉列表被正确呈现.当我点击提交时,我收到了这个奇怪的错误:
WicketMessage: Method onFormSubmitted of interface
org.apache.wicket.markup.html.form.IFormSubmitListener targeted at component
[MarkupContainer [Component id = fittest]] threw an exception
Root cause:
java.lang.IllegalStateException: Attempt to set model object on null
model of component: testform:siteid
at org.apache.wicket.Component.setDefaultModelObject(Component.java:3033)
at
org.apache.wicket.markup.html.form.FormComponent.updateModel(FormComponent.java:1168)
at
[snip]
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚什么是null.它渲染得很好,所以它找到了对象.我错过了什么?
tpd*_*pdi 12
好吧,你没有显示你的SiteList类的代码,但正在发生的事情是 - 几乎肯定是下拉列表 - 没有模型.因此,当wicket调用时,dropdown.getModel().setModelObject( foo ) ;它本质上会获得一个空指针异常.
我的建议就是这样,遵循旧的OO经验法则更喜欢组合继承.您SiteChoice和SiteList类似乎没有添加太多,并且它们使您的错误更难调试.
相反,只需在表单中添加DropDownChoice:
form.add( new DropDownChioce( "siteid",
new Model<DomainObject>(),
new ChoiceRenderer<DomainObj>("name", "URL") );
Run Code Online (Sandbox Code Playgroud)
那也更简洁,