如果在加载模型时发生错误,向用户呈现该错误的正确方法是什么?似乎向页面添加反馈组件并使用 error() 是执行此操作的正确方法,但是当我这样做时,出现此错误:
Caused by: org.apache.wicket.WicketRuntimeException: Cannot modify component hierarchy after render phase has started (page version cant change then anymore)
at org.apache.wicket.Component.checkHierarchyChange(Component.java:3572)
at org.apache.wicket.Component.addStateChange(Component.java:3501)
at org.apache.wicket.Component.error(Component.java:1254)
at com.prosc.wicket.LoadableDetachableModelErrorTest$1.load(LoadableDetachableModelErrorTest.java:21)
at com.prosc.wicket.LoadableDetachableModelErrorTest$1.load(LoadableDetachableModelErrorTest.java:17)
at org.apache.wicket.model.LoadableDetachableModel.getObject(LoadableDetachableModel.java:121)
at org.apache.wicket.Component.getDefaultModelObject(Component.java:1605)
这是我的Java代码:
package com.prosc.wicket;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.LoadableDetachableModel;
public class LoadableDetachableModelErrorTest extends WebPage {
public LoadableDetachableModelErrorTest() {
add( new FeedbackPanel( "feedback" ) );
add( new Label("dynamicText", new LoadableDetachableModel<String>() {
@Override
protected String load() {
String result = "Hello World";
error( "Uh oh, some imaginary problem happened" );
return result;
}
}));
}
}
这是我的 HTML 标记:
<html>
<body>
<div wicket:id="feedback" />
<div wicket:id="dynamicText" />
</body>
</html>
预期的结果是看到向用户显示错误消息的空白页面。