如何在Wicket中动态修改Wicket 1.6页面的body onload标签?
我想在“onload”属性中添加一个JavaScript函数,该属性需要我用Java生成的动态参数。
提前致谢
答案 0 :(得分:1)
最好使用以下方法。
在需要执行某些onLoad操作的任何组件中,添加以下方法:
@Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
response.render(
OnDomReadyHeaderItem.forScript("alert('REPLACE ALERT BY YOUR SCRIPT')"));
}
如果您需要使用额外的参数等动态编写脚本,我建议遵循:
private final static TextTemplate template
= new PackageTextTemplate(YourComponentOrPage.class, "your-js-template.js");
@Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
Map<String, Object> params =new HashMap<String, Object>();
params.put("parameter1", parameter1Value);
params.put("parameter2", parameter2Value);
response.render(OnDomReadyHeaderItem.forScript(template.asString(params)));
}
“your-js-template.js”可能如下所示:
alert('Hi!, ${parameter1}. My name is ${parameter2}');
答案 1 :(得分:0)
目前Wicket 6.x有一个特定的onLoad事件类。不再需要任何hackish解决方案:
@Override
public void renderHead(IHeaderResponse response)
{
super.renderHead(response);
response.render(OnLoadHeaderItem.forScript(yourDynamicScript));
}