adding external js in gwt - java

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.

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>

Related

cant read files from resources

Im trying to read files(xml, images) from src/main/resources. But it doesnt work.My resources folder is Source folder. For example, when i m trying to read log4j.xml tomcat looking for it in C:/bin... And i also cant read images from resources. I can read it only from webapp. I ve read, that tomcat automatically replace files from resources to webinf, but i think that it doesnt work in my case.
Please, help. I dont have any idea.
Even if the png is added to the WEB-INF folder it wouldn't be accessible from the JSP page in your case.
On runtime a JSP page renders into a servlet that return HTML code in the response. Then a browser will parse your tag <img src="/Pajero.png" ... /> and send a request onto YOUR_HOST/Pajero.png url to access the image and get the error code 404, because content of the WEB-INF folder is not accessible for a client side in a java web application.
Perhaps it will be better to place images on the webapp folder at your case.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("log4j.xml").getFile());
You can use something like this to get resources from classpath.
Regards

Smart GWT app displays nothing because nocache.js file not found error

I have a smart GWT app example called AwesomesmartGWTUIProject from
javacodegeeks
Its package is com.javacodegeeks.smartgwt.appui
In eclipse when i try to run it as a web application I get the following error
In order for your application to run correctly, you will need to include these tags in your host page directly. In order to avoid this error, you will need to remove the script tags from the gwt.xml file, or add this property to the gwt.xml file: <set-configuration-property name='xsiframe.failIfScriptTag' value='FALSE'/>
[ERROR] shell failed in doSlowStartup method
Mar 31, 2014 10:07:41 PM com.google.appengine.tools.development.LocalResourceFileServlet doGet
WARNING: No file found for: /awesomesmartgwtuiproject/com.javacodegeek.smartgwt.appui.awesomesmartgwtuiproject.nocache.js
It simply doesn't display anything when I try to run it in my browser. Am new to smartGwt so am not so conversant with the configurations. Any ideas ?
Have you compiled your GWT project?
Is there nocache.js file generated in your project at correct location?
You have to read about Understanding the GWT Compiler.
Every thing is defined at above link including project structure, GWT compilation etc.
Use rename-to attribute in your module gwt.xml file.
Directly from the above link:
You may have noticed that one of the generated files is named after your module, followed by a .nocache.js suffix. This is the GWT bootstrap file. Similar to the output subdirectory war/, the name of this file is also controlled by the rename-to attribute in your module XML file.

Linking Files And Directory Structuring

I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.
Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.
Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.
Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.

Cannot Reach Html File on Google App Engine

I have an Eclipse Java project, and I added a "folder" off of the root called "webfiles". I then proceeded to create a file called form.html. Once I run the project locally or publish it, I cannot reach the .html file. I get a Not_Found error.
Is it possible in Google App Engine to use HTML files (other than index.html) and where do I have to place them in the project to access them by a browser? What path should I use in the browser.
You need to add include path="/**.html" " in your static files definition in appengine-web.xml else html files other than index.html would not even be uploaded.
If you have /war/webfiles/form.html in your project than path to acces it from browser:
http://your_app_name.appspot.com/webfiles/form.html
To make reference from other jsp pages use:
link to form

How to change location of static file in WAR archive?

By default static files are located in WEB-INF directory (accessible as /images/logo.png):
foo.war
WEB-INF
web.xml
images
logo.png
I want to change the structure and use this one instead (still accessible as /images/logo.png):
foo.war
WEB-INF
web.xml
static
images
logo.png
How can I do this with web.xml?
The container will repsond with a 404 NOT FOUND error if you directly access the files under WEB-INF using HTTP GET .
But now , you said you can access WEB-INF/images/logo.png by /images/logo.png , so I think your web application most probably achieve this result by some URLRewriteFilter mechainsim or by some Java code in the servlet level (eg a filter) , or by your web application 's framework . I suggest you to check your web application to see what mechanism causes this behvaior now and configurate to your desired result accordingly.
According to http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WCC3.html,:
A WAR has a specific directory
structure. The top-level directory of
a WAR is the document root of the
application. The document root is
where JSP pages, client-side classes
and archives, and static Web resources
are stored.
The document root contains a
subdirectory called WEB-INF, which
contains the following files and
directories:
web.xml: The Web application
deployment descriptor Tag library
descriptor files (see Tag Library
Descriptors) classes: A directory
that contains server-side classes:
servlets, utility classes, and
JavaBeans components lib: A
directory that contains JAR archives
of libraries (tag libraries and any
utility libraries called by
server-side classes).
You can also create
application-specific subdirectories
(that is, package directories) in
either the document root or the
WEB-INF/classes directory.
So the default behavior is what you're looking for. Is your document root set incorrectly to serve content from WEB-INF?
You may use a filter or URLRewriteFilter to point /images/* to /static/images/*.
If you just want your folder structure to be /static/images for development time organization purposes, but the deployment URL to be /images -- you may need to alter your build script to copy /static/** to /.
I personally would not bother whether my static files are referred as /static/images or /images -- because they would be referred in my code (only), which I have control over.
If you are using these files in CSS and that's why you wanted the path to stay the same... better keep the images under /static/css/images and have the images that are referred in the CSS here. In this way, no matter where you move your CSS folder, you would not bother spoiling your CSS.

Categories

Resources