admin管理员组

文章数量:1290232

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>

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>
Share Improve this question asked Dec 2, 2013 at 6:10 P0ulBogdP0ulBogd 771 silver badge12 bronze badges 1
  • you can add scripts into index page. – robert lennon Commented Dec 2, 2013 at 11:20
Add a ment  | 

2 Answers 2

Reset to default 8

To use this technique you have to place your someJsFile.js in your public folder so as the gwt piler 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 piler reads the javascript file and includes the content in the final piled 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 piler can optimize and obfuscate it.

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

You can define in your_project.gwt.xml which folders to include as public. The paths should be relative to the xml:

resources/
 |-your_project.gwt.xml
 |-subfolder/
   |-stuff/
       |-images/
       |-js/
          |-someJsFile.js

In your xml add:

<public path="subfolder/stuff" />

This should copy images/ and js/ folders into your webapp directory and you can use sth like this for the js file

<script src="js/someJsFile.js"></script>

本文标签: javaadding external js in gwtStack Overflow