Set path for js files generated by GWT - java

I have GWT application and I need to use files, generated for client side in Java EE application that doesn't use GWT. Actually, I just want to include my_app.nocache.js in some html page.
This part works fine, problems begin when my_app.nocache.js tries to load other resources, generated by GWT (other *.js files and *.css files). But I have all my resources in /_resources/mypackage/ and my_app.nocache.js doesn't know anything about this and tries to load just my_name.css instead of /_resources/mypackage/my_name.css
Is there any way to set up path for GWT to generate resource files, so it wold be same, as I use in my Java application?

Related

Given a JAR file, allegedly contains web server, how do I deploy it?

I've been handed a JAR file, told it contains a web application, and instructed to deploy it. I'm familiar with java as a language, but I have next to no experience with its web ecosystem. Trying to run it as an executable, I find out it has no main class, so I take a look at the contents, and find that it has a META-INF/web-fragment.xml file, along with a couple of Servlet classes and some config files referenced in the xml. It looks like there is basically everything here for a complete application, but I have no idea how to actually deploy it, and google only yields tutorials on how to build these things from scratch with IDEs and deploy using maven plugins and goals.
Assuming that building from source is out of the question and I only have standard unix and java CLI tools available, based on the information provided (And I can provide more if necessary), what is the simplest way to get this up and running?
A web applicaton should be in a WAR file, not a JAR file, and you should only need to just drop it into the webapp directory of your Tomcat, or follow the instructions for whatever other container you're using.
If it's only a fragment, as appears from the XML, (a) it isn't a webapp, only part of one, and (b) you personally can't deploy it at all. You need the whole thing.
Send the assignment back for clarification.

When converting from Java, can gwt generate .js instead of .html files?

I find that GWT will generate multiples HTMLS and use frame injection to invoke the JS logic in the caller HTML. But most of the browsers will not allow you to do frame injection on a local file system.
I am trying to avoid accessing different htmls on a local file system. Is there any way to generate pure JavaScript files from GWT instead of HTML ones?
Thanks!
Yes, you just have to change the linker.
Add this line in your *.gwt.xml module file to have pure .js files:
<add-linker name="xs"/>
Or this one, if you want .js files but they are executed in an iframe context.
<add-linker name="xsiframe"/>

Static resources in GWT apps

Do you ever need to write your own HTML, CSS or JavaScript files for your GWT app? Any reason to ever write your own HTML/CSS and include it with the build? If so, under what circumstances, and where do you package it in the final WAR?
Some developers prefer to use an external CSS file for their app instead of a CSS resource. Then you add this CSS file to your /war directory, and provide a link to it in your host page like you would do with any external CSS file.
If you use UI:Binder, which I highly recommend, you can write a lot of page or widgets' layouts in HTML as opposed to using GWT widgets. In this case you don't have to do anything with the HTML: GWT compiler will take care of it.
There is a way to use native JavaScript from inside the GWT app. Sometimes developers want a special effect or some functionality that they can't (or, quite often, don't know how to) implement in GWT. In this case you include the JavaScript code directly into your GWT code, or you use an external JavaScript file and reference it from GWT. In the latter case you include your JavaScript file in your /war directory and load it in your host page like with any JavaScript file.

Java EE Application Directory Best Practice Question

I am relatively new at Java EE and have just gotten familiar with the traditional application directory structure (which includes the BUILD, DIST, NBPROJECT, SRC, and WEB folders). I was wondering: if my application requires to use some resources/assets (such as images or pdfs) on the server, in which of the 5 folders listed above should I place those resources? I think I can technically place them anywhere, as long as I reference the files correctly, but not sure what the common best practice is for this.
You would usually include static content e.g. images or PDFs which need to be read by http requests in WebContent e.g. if my-web is your WAR project:
my-web/WebContent/images
my-web/WebContent/scripts
my-web/WebContent/misc
This means you can access using for example:
/my-web/images/bob.jpg
/my-web/scripts/bob.js
/my-web/misc/bob.pdf
Storing the files here allows public access. I will quote this from the Eclipse help pages:
The Web content folder represents the
contents of the WAR file that will be
deployed to the server. Any files not
under the Web content folder are
considered development-time resources
(for example, .java files, .sql files,
and .mif files), and are not deployed
when the project is unit tested or
published.
You need to think about how the assets get delivered to the client before deciding where to put them. Resources such as ccs and static html pages are retrieved by the client using separate HTTP calls, so they need to be in a place that is accessible. In your case, I'd say they have to be at the level of index.jsp, or in subdirectories at that level. If you put them in WEB-INF, they will be inacessible from the outside world. You could possibly put them in META-INF but it would be an unusual use of that directory.

Using Java code on embedded HTML instead of JavaScript?

I am working on a project which is basically a Java application with an embedded IE browser (using JDIC 0.9.5) to display custom HTML files (stored locally that I created). I have a test HTML file with a JavaScript function that checks a simple form with checkboxes and alerts the user with a dialog stating which checkboxes are checked.
My question is, is there a way for my Java application to do the same procedure on the embedded HTML form instead of using JavaScript. I want to keep my application and HTML files simple without the clutter of JavaScript in my HTMLs or a pile of .js files.
Thanks for any help and guidance!
You have two options. Either shift the project to run the Java on the server side using JSP technology inside a web container like Apache Tomcat or Jetty, or write you web page to open up a Java applet.
The applet route allows you to run the code on someone else's machine, and as a trade off you will have to run the application in a strongly security constrained environment. After all, if someone were to run code on your machine, you wouldn't want it able to access your disk, etc.
The JSP solution will have you running the code on the same machine as your web server, since you (probably) control your own web server, the code will not be ran with as many security constraints enabled. This means the code can make requests to other machines, write and read files, etc.
You could replace your model with a client-server model using Java Server Pages (JSP).
If you are displaying the page through an embedded browser it is very unlikely that you will be able to get access to the DOM via Java.
One option is to use GWT javascript compiler to code in java and then convert to javascript. If it will only use IE, then you will only need to keep one of the generated .js files, so the clutter will be low.
Since you're embedding your HTML files in your own Java program, I would recommend you to use one of these 2 approaches:
1.- Use Javascript and structure the files cleanly, is not that complex.
2.- When you do the POST check the values in your Java code and return a new dynamically generated HTML file with needed info
I sincerely recommend you to follow the first option. Other options to work with Java in HTML would be JSP or GWT, but both will require a proper J2EE server, which would be overkill in your application

Categories