在gwt中添加外部js [英] adding external js in gwt

查看:84
本文介绍了在gwt中添加外部js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了GWT中的js代码。
对于检查,我在(projectName).html文件中添加了我的js代码,它工作。

但是当我尝试添加外部js文件时,出现错误:

  WARN] 404  -  GET< js文件的路径> someJsFile.js(127.0.0.1)1452字节
请求标题

我将此行添加到(projectName).gwt.xml文件中:

 < script src =src / main / resources /< projectName> /someJsFile.js>< / script> 


解决方案

要使用这种技巧,您必须将<$在您的 public 文件夹中添加c $ c> someJsFile.js ,以便gwt编译器将其复制到最终文件夹,在该文件夹中放置html和js东西。

如果您使用的是maven,您必须检查资源插件是否将此文件复制到战争中以及在哪个路径中。

顺便说一下,还有其他技术可以在您的文档中插入外部JavaScript:


  • 将脚本标记放置在.html文件中。

  • 使用 ScriptInjector .fromUrl()。

    更好的方法是使用 TextResource ScriptInjector.fromString(),以便编译器读取JavaScript文件并将内容包含在最终编译的文件中。

    p>您可以使用gwtquery Ajax.getScript 通过ajax获取脚本并将其注入到dom中。


  • A 新的方式,最近添加到gwtquery,允许你包含你的JavaScript作为一个JSNI块,所以编译器可以优化和混淆它。




我宁愿最后一个,因为它提供了更多的优点。 / p>

I writed and checked my js code in GWT. For checking I added my js code in (projectName).html file and it is worked.

But when I try added external js file I get an error:

WARN] 404 - GET <path to js file>someJsFile.js (127.0.0.1) 1452 bytes
         Request headers

I added this line to (projectName).gwt.xml file:

<script src="src/main/resources/<projectName>/someJsFile.js"></script>

解决方案

To use this technique you have to place your someJsFile.js in your public folder so as the gwt compiler copy it to the final folder where it places the html and js stuff.

If you are using maven you have to check if the resources plugin is copying this file to the war and in which path.

By the way, there are other techniques to insert external javascript in your document:

  • Placing the script tag in your .html file.

  • Using ScriptInjector.fromUrl().

  • A better approach is to use a TextResource and ScriptInjector.fromString() so as the compiler reads the javascript file and includes the content in the final compiled file.

  • You can use gwtquery Ajax.getScript to get the script via ajax and inject it in the dom.

  • A new way, recently added to gwtquery, allows you to include your javascript as a JSNI block, so as the compiler can optimize and obfuscate it.

I'd rather the last one because it offers much more advantages.

这篇关于在gwt中添加外部js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆