Reputation: 5153
I am building a GWT app. Previously, whenever I requested an image from the web-page, that request went to a client-class, and that class used to serve the image. This worked for both GWT generated URL as well as the standalone file URL after compilation.
But now I have replaced that part with a Ajax (RPC) call to the server, where the serverside class is receiving the necessary parameters from the client-class, and serving the image, which is being sent by the client-class to the UI. This works fine with GWT generates URL, but after compilation, when I am trying to run it as a standalone HTML (by giving the path to the file in the URL bar), no Ajax request is fired.
Is is because the RPC call needs a server to respond to (in contrast to jQuery Ajax calls, which work jolly well in desktop alone)? How can I mimic the Ajax behavior in Desktop mode also? The call looks something like this:
private final GreetingServiceAsync response = GWT.create(GreetingService.class); //(I haven't changed the defualt names..:))
response.greetServer(i, j,new AsyncCallback<String,String>() { // i,j is already calculated, server needs to know these to pass an image url
public void onSuccess(String url1, String url2) {...}
public void onFailure(Throwable caught) {...}
});
Upvotes: 2
Views: 1450
Reputation: 2747
This should be feasable. You have to implement the onFailure, because it will be called if no server is available.
Make a new Class for AsyncCallback something like this, (by default AsyncCallback has only one parameter, have you implemented it with two?):
public class UrlCallback implements AsyncCallback<String, String> {
private String url1;
private String url2;
public UrlCallback(String url1, String url2) {
this.url1 = url1;
this.url2 = url2;
}
@Override
public void onSuccess(String result1, String result2) {
//"Do what you want to do here"
}
@Override
public void onFailure(Throwable caught) {
//Respond the static file here
}
}
I handle it in my case, to server image-urls from localstorage when i have no internet connection:
public class PictureCallback implements AsyncCallback<Picture> {
private Image picture;
private IAppRequestTransportSupport storage;
private String storeId;
public PictureCallback(String storeId, Image picture) {
this.picture = picture;
this.storage = new AppLocalStorageSupport();
this.storeId = storeId;
}
@Override
public void onSuccess(Picture result) {
picture.setUrl(result.getImageUrl());
storage.doOnSuccess(result.getImageUrl(), "picture"+storeId);
}
@Override
public void onFailure(Throwable caught) {
try {
String pic = storage.readFromLocaleStorage("picture"+storeId);
if(pic != null && !pic.equals("")) {
picture.setUrl(pic);
}
} catch (KeyNotFoundException e) {
e.printStackTrace();
} catch (NoLocaleStorageSupportException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 121998
You completely came out of the GWT structure
.
Once you compile your project the all GWT code coverts into JavaScript.
Even though there is no server running and if you accessed your html file from file system like C://myapp/myapp.html . the browser will serves that as a static web
page ..ofcourse inside that html page there will be your app.nochahe.js
which is pure javascript .
So with out any hesitations the browser displays the all content ..but it wil never become an so called web application and it never make any ajax or any other server
call.
In your case you are not running any server and accessing them as a static pages
and expecting them to connect server
and bring your data which is quite impossible .
So first of all please run||debug your code in development mode.
After started running or Debugging the project ..the generated url in the development mode tab will look like below .
h t t p : / / localhost : 8888 / MyModule.html ? gwt.codesvr = localhost : 9997
You may have a doubt regarding the parameter gwt.codesvr.
It runs your client-side Java code, which is compiled to class files, but not yet to JavaScript files.
Once done with your implementations compile the project and export you war folder on any server to test or access and access them as
ex:localhost:8080/myapp/someservice.
Coming to the so called AJAX calls ,They are RPC's in the GWT .RPC is the GWT internal structure to communicate with the server ,normally they are all impl classes in general ,those extends RemoteServiceServlet which serves the data to client on HTTP
protocol and impossible to evoke them without running server.
If you still have a confusion about different GWT
application modes refer this Differences link
Upvotes: 2
Reputation: 4363
You mean if you just open HTML host page directly from the file system? This can not work, since you don't have a server then. That way there is no server-side which could answer your RPC call. You have to run your GWT app in a servlet container (like Tomcat or Jetty), so that the server-side RPC servlet is running and ready to answer the RPC calls from the client.
Even if you are running a server somewhere. The RPC call can not not where to find the server, if you just open the file from the file system. The RPC call uses the URL (host page base URL) to locate its server. In your case this is file://C/something
instead of http://www.hererunsaserver.com/
.
You could probably embed your data within the app, to achieve some kind of desktop mode. But I don't know whether this is what you are up to?
Upvotes: 1