Help! Displaying helpful data on a form.

0 views
Skip to first unread message

David C. Hicks

unread,
Nov 16, 2009, 5:02:36 PM11/16/09
to GWT Users
I'm sure I must be making this harder than it needs to be, but I'm
having zero success doing it.

Entering a new form, I'd like to display the name of an entity that was
selected on the the calling page. The calling page is a Struts2 JSP, so
I don't have the entire entity. I only have the entity's ID, passed in
as a parameter on the request URL. That's easy enough to get, and I
even have a service set up to go get the full entity once I have the
ID. The problem is that the service is asyncronous. So, I get a Null
Pointer Exception at the point where I try to set the data I'm
retrieving into a label because the call hasn't yet completed.

I'd rather not pass the name on the request URL - trying to avoid the
messy encoding/decoding problems. I've tried to create an AsyncCallback
class that included a hook that I could "wait" for, but this only seemed
to create an infinite wait loop.

So, now I'm wondering if I need to somehow "bind" the retrieved data to
a field on my form so that it will populate when the call completes.
Then again, maybe this is overkill, too.

Is there a better way to solve this problem?
Thanks,
Dave

David C. Hicks

unread,
Nov 16, 2009, 5:58:39 PM11/16/09
to GWT Users
Nevermind. I figured out that I could update my label on kind of an
asyncronous basis, as well.

Davis Ford

unread,
Nov 16, 2009, 11:17:11 PM11/16/09
to google-we...@googlegroups.com
Are you using GWT-RPC?  I'm not sure I understand your problem completely.  

So if I have a service like:

public interface MyService extends RemoteService {
   public Entity fetchEntity(String id);
}

public interface MyServiceAsync {
    void fetchEntity(String id, AsyncCallback<Entity> callback);
}

then, in the GWT code, something triggers the service.  Typically it is via a button click or some event.  You seem to indicate that you are doing it by passing the id as a URL parameter, like:


You should be able to parse the URL using the Window.Location.getPath() to get the url param with a regex.

Once you have that, you do this:

MyServiceAsync service = GWT.create(MyService.class);

service.fetchEntity(id, new AsyncCallback<Void>() {
     onFailure(Throwable t) {
          Window.alert("service call failed: "+t.getMessage());
     }
     onSuccess(Entity e) {
         // do something with the entity
     }
});

After onSuccess( ) -- you populate the form with whatever.  


--

You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=.





--
Zeno Consulting, Inc.
home: http://www.zenoconsulting.biz
blog: http://zenoconsulting.wikidot.com
p: 248.894.4922
f: 313.884.2977

David C. Hicks

unread,
Nov 16, 2009, 11:34:42 PM11/16/09
to google-we...@googlegroups.com
Hi Davis,

Yes, using GWT-RPC.  My problem was that I "forgot" that I'm dealing with an asynchronous call.  I was trying to populate the field on my form with data before the call had successfully returned.  So, I was getting a Null Pointer Exception from the object I was fetching.  Once I realized this, I moved my code to populate the field into the onSuccess() method and it's all happy.

Thanks for the reply!
Dave
Reply all
Reply to author
Forward
0 new messages