简体   繁体   English

Wicket 6:包括Javascript文件以script标签开头

[英]Wicket 6: Including Javascript files to head with script tag

I guess the title is very self descriptive but here it goes nonetheless: I have a Panel where I would like to add multiple script tags with javascript files from my system. 我想标题是很容易描述的,但是尽管如此,它仍然可以使用:我有一个面板,我想在其中添加来自系统中带有javascript文件的多个脚本标签。

I tried the following but script tags still not showing. 我尝试了以下操作,但脚本标记仍未显示。

@Override
public void renderHead(IHeaderResponse response) {
  super.renderHead(response);
  for(String path : arrayOfJsFiles){
    response.render(JavaScriptHeaderItem
        .forReference(new PackageResourceReference(getClass(),
                path)));
  }

} }

Without knowing the exact file hierarchy of your project I can't tell what exactly could the issue be, but I can give you some pointers. 在不知道项目的确切文件层次结构的情况下,我无法确定问题的确切原因,但是我可以为您提供一些指导。

Firstly, if I understand correctly, the javascript files are not in the same package as the class in which the code you supplied is executed. 首先,如果我理解正确,则javascript文件与执行您提供的代码的类不在同一个程序包中。 That is not quite what the PackageResourceReference expects. 这不是PackageResourceReference期望的。 Package resource is a resource that is in the package of the file; 资源是文件中的资源。 so if you have a class com/custom/MyClass.java, your package resources should be also be located in the same folder, ie com/custom/some-javascript.js 因此,如果您使用com / custom / MyClass.java类,则您的包资源也应位于同一文件夹中,即com / custom / some-javascript.js

For that reason in the constructor PackageResourceReference#PackageResourceReference(Class, String) you supply 2 things: the scope and the name . 因此,在构造函数PackageResourceReference#PackageResourceReference(Class, String)您需要提供两件事: 作用域名称 The scope is a class that is in the same package as the resources, the name is simply the name of the file, rather than the path to the file; 作用域是一个与资源位于同一包中的类,名称仅是文件的名称,而不是文件的路径; the path is defined by the scope. 路径由范围定义。

I'm not sure if it is the best approach, but when I need to use global resources, I don't actually ever construct a ResourceReference , but instead use JavaScriptHeaderItem.forUrl("./js/some-javascript.js") and such to render the resources (./js/ is the webapp/js/ folder). 我不确定这是否是最好的方法,但是当我需要使用全局资源时,我实际上并没有构造ResourceReference ,而是使用JavaScriptHeaderItem.forUrl("./js/some-javascript.js")并呈现资源(./js/是webapp / js /文件夹)。

However, I'm not sure if that is a correct way to deal with resources. 但是,我不确定这是否是处理资源的正确方法。 I highly recommend reading this , as it covers resources quite well and could help you with your problem. 我强烈建议您阅读此书 ,因为它涵盖了很多资源,可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM