根据 jordeu 的回答和 Wicket wiki,我做了这个。它不需要修改组件组件层次结构,但在我的情况下,面板已经是页面的一部分。
但是我有点担心更改应用程序设置和使用 Wicket API 的非公共部分。
public static StringResponse getRenderedResponse(final Component container) {
// store rendered markup to different place
final Response originalResponse = RequestCycle.get().getResponse();
StringResponse stringResponse = new StringResponse();
RequestCycle.get().setResponse(stringResponse);
// disable "component must be rendered only once per request" -check
boolean originalComponentCheck = container.getApplication().getDebugSettings().getComponentUseCheck();
container.getApplication().getDebugSettings().setComponentUseCheck(false);
try {
// prepareForRender() is not part of public Wicket API!!
container.prepareForRender();
container.render();
container.afterRender();
} finally {
// restore RequestCycle and checks
container.getApplication().getDebugSettings().setComponentUseCheck(originalComponentCheck);
RequestCycle.get().setResponse(originalResponse);
}
return stringResponse;
}
用法:
new AjaxLink() {
public void onClick(AjaxRequestTarget target) {
StringResponse response = getRenderedResponse(myPanel);
String myPanelHtml = response.toString();
// do stuff with myPanelHtml
}