通过 wicket 动态编写脚本 src

yoa*_*str 2 wicket

我希望我的页面动态加载 javascript 到我的身体

<script type= "text/javascript" src="this path should be decided from wicket dynamically"/>
Run Code Online (Sandbox Code Playgroud)

我使用的是 wicket版本 1.4,因此JavaScriptResourceReference 在我的版本中不存在(我检查它不是 ' )

我该如何解决这个问题?提前致谢 :)。

rot*_*sch 5

我将我的评论指定为答案。

您可以使用此代码片段:

WebMarkupContainer scriptContainer = new WebMarkupContainer("scriptContainer ");
scriptContainer .add(new AttributeAppender("type", Model.of("text/javascript")));
scriptContainer .add(
    new AttributeAppender("src", urlFor(
        new JavaScriptResourceReference(
            YourClass.class, "JavaScriptFile.js"), null).toString()));
add(scriptContainer );
Run Code Online (Sandbox Code Playgroud)

和相应的html:

<script wicket:id="scriptContainer "></script>
Run Code Online (Sandbox Code Playgroud)

只需更改字符串JavaScriptFile.js即可加载任何其他 Javascript 文件。