我正在学习wicket框架,在使用ajax时,我尝试了一个示例,其中我想显示一个带有所选单选按钮公司名称的面板。但在这样做时我遇到了一些错误。在这里,我提到了编码中的所有细节:
Test.java
public class Test extends WebPage {
private static final List<String> companies = Arrays.asList(new String[] {"Google","Microsoft","Yahoo"});
private ArrayList<String> compSelected = new ArrayList<String>();
private Panel panel;
public Test(final PageParameters parameters) {
final RadioChoice<String> companyList =
new RadioChoice<String>("companyNames",new Model(compSelected),companies);
companyList.add(new AjaxFormChoiceComponentUpdatingBehavior() {
@Override
protected void onUpdate(AjaxRequestTarget target) {
if("Google".equals(companyList.getModelObject())){
panel = new Panel1("panel1","Google");
}
else if("Microsoft".equals(companyList.getModelObject())){
panel = new Panel1("panel1","Microsoft");
}
else{
panel = new Panel1("panel1","Yahoo");
}
panel.setOutputMarkupId(true);
target.addComponent(panel);
}});
Form<?> form = new Form<Void>("form");
add(form);
form.add(companyList);
form.add(panel);
this.add(form);
}
}
的test.html
<form wicket:id="form">
<label><b>Company List:</b></label><br>
<span wicket:id="companyNames">radio button list will goes here</span><br>
<span wicket:id = "panel1" class="panel">panel goes here</span>
</form>
Panel1.java
@SuppressWarnings("serial")
public class Panel1 extends Panel{
public Panel1(String id,String cName) {
super(id);
add(new Label("msg", "selected company is:"+ cName));
}
}
Panel1.html
<body>
<wicket:panel>
<span wicket:id="msg">message goes here</span>
</wicket:panel>
</body>
WicketMessage:无法使用构造函数public demo.wicket.ajax.Test(org.apache.wicket.PageParameters)和参数根本原因实例化页面:java.lang.IllegalArgumentException:参数child在org中可能不为null .apache.wicket.MarkupContainer.add(MarkupContainer.java:131)位于org.apache的java.lang.reflect.Constructor.newInstance(未知来源)的demo.wicket.ajax.Test。(Test.java:46)。 wicket.session.DefaultPageFactory.createPage(DefaultPageFactory.java:188)org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:65)at org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.newPage(在org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.processEvents(BookmarkablePageRequestTarget.java:org.apache.wicket.request.target.component.BookmarkablePageRequestTarget.getPage(BookmarkablePageRequestTarget.java:321)上的BookmarkablePageRequestTarget.java:299) 234)在org.apache.wicket.request。位于org.apache.wicket的org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)的AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92) .RequestCycle.steps(RequestCycle.java:1428)在org.apache.wicket.RequestCycle.request(RequestCycle.java:545)在org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:468)在org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:301)位于org.apache.catalina.core.ApplicationFilterChain的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)。 org.apache上的org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)中的doFilter(ApplicationFilterChain.java:208)org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) .catalina.authenticator.AuthenticatorBase.invoke(Authenticato rBase.java:503)atg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)atg.apache.catalina .valves.AccessLogValve.invoke(AccessLogValve.java:950)org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) )org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:611)at org.apache.tomcat.util.net。 JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:314)在java.util.concurrent.ThreadPoolExecutor中的$ Worker.runTask(来源不明)在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(来源不明)在org.apache.tomcat java.lang.Thread.run中的.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) nknown来源)
我不确定我在这里做错了什么。有人可以帮我解决一下吗?
答案 0 :(得分:0)
问题是form.add(panel);
。那时panel
为空。您在#onUpdate()
中初始化它,但只有在单选按钮上单击(?)后才会执行。
解决方案:
将panel
类型更改为WebMarkupContainer
并在页面的构造函数中初始化它。同样在构造函数中调用panel.setOutputMarkupId(true)
,以便<span wicket:id="panel1"...>
在呈现的HTML中具有id
,并且稍后可以使用JS来替换/更新它。在#onUpdate()
中使用类似panel = panel.replaceWith(new Panel1(panel.getId(), choice.getModelObject())); target.add(panel);