Wicket 1.5 Javascript File in Body Tag

左心房为你撑大大i 提交于 2019-12-01 07:45:51

问题


I need to put a script-tag inside the body, because the javascript doesn't work in the head tag.

As of Wicket 1.5 the following code is not longer supported:

add(new JavaScriptReference("wz_tooltip", new JavaScriptResourceReference(BaseTemplate.class, "wz_tooltip.js")));

This is because the class JavaScriptReference no longer exists.

I can't obviously use the new renderHead method.

My workaround so far:

WebMarkupContainer script = new WebMarkupContainer("script");
script.add(new AttributeAppender("type", Model.of("text/javascript")));
script.add(new AttributeAppender("src", Model.of("wz_tooltip.js")));
add(script);

Really ugly apart from that it doesn't work; can't find the javascript files.

Is there any other workaround or a "right" way to do it?


回答1:


Have you tried using a urlFor with a resource reference? Something like..

script.add(new AttributeAppender("src", urlFor(new JavaScriptResourceReference(BaseTemplate.class, "wz_tooltip.js"), null).toString())); 

I wouldn't expect your current method to work out of the box because of Wicket's complex resource management. Also, while there may be other "right" ways to do things, I've often found that 3rd-Party javascript can be finicky about placement and I still need to fall back on this method for some scripts.

For example, one script I use must be the last element on the page.



来源:https://stackoverflow.com/questions/7993626/wicket-1-5-javascript-file-in-body-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!