Question

I'm currently working on a simple GWT project. One of the things I'd like to do is that when the page loads I can dynamically populate the contents of a ListBox based on certain criteria. I actually don't see any handlers for a ListBox to handle the initial render event but I see change handlers.

How does one populate a ListBox contents with data from the server side on pageload with GWT?

Right now I have a class that implements EntryPoint that has a

final ListBox fooList = new ListBox();

I also have a set of beans but I also have a class implementing RemoteService. Since I can't seem to get direct calls to my user defined packages directly in the EntryPoint (which makes sense) how do I populate that ListBox with server side content on initial page load? Right now I'm using a List but I figure if I cant get that to work I can get a DB call to work...

I've tried things in the EntryPoint like:

    for (String name : FOOS) {
        fooList.addItem(name, name);
    }

However FOOS would derive from a server side data and the EntryPoint is supposed to be largerly limited to what can compile to JS! I can't get user defined classes to be recognized on that side as that string is the result of a set of user defined classes.

I also tried creating a method in the class implementing RemoteService that returns a ListBox. This also didn't compile when I tried to call this method. Perhaps I don't fully understand how to call methods in a RemoteService service implementing class.

I've searched a lot and I can't find anything that clearly explains the fundamentals on this. My background is much more ASP.NET and JSPs so perhaps I'm missing something.

I'm using GWT 2.6 is that is relevant.

Was it helpful?

Solution

The usual procedure is the following:

  1. Create a bean class for the data you want to transmit between client and server. Let's call it MyBean.
  2. Place MyBean in the shared package of your project.
  3. This class has to implement either Serializable or IsSerializable, otherwise GWT will complain that it doesn't know how to transmit it.
  4. Create your RemoteService that contains the method you want to use to transmit MyBean from/to the server.
  5. Once you get your data on the client using an AsyncCallback and your RemoteService, fill the ListBox using your beans, e.g. by calling MyBean#getName() or MyBean#toString().
  6. Success!

OTHER TIPS

I based my example on the GWT sample project ( I named it example), just replace the classes and it should work :

public class Example implements EntryPoint {
/**
 * Create a remote service proxy to talk to the server-side Greeting
 * service.
 */
private final GreetingServiceAsync greetingService = GWT
        .create(GreetingService.class);

/**
 * This is the entry point method.
 */
public void onModuleLoad() {

    final ListBox listBox = new ListBox();

    RootPanel.get("sendButtonContainer").add(listBox);

    greetingService.getSomeEntries(new AsyncCallback<String[]>() {

        @Override
        public void onSuccess(String[] result) {
            for (int i = 0; i < result.length; i++) {
                listBox.addItem(result[i]);
            }

        }

        @Override
        public void onFailure(Throwable caught) {

        }
    });

}
} 

This is our EntryPoint, it creates a listbox and calls the server with a AsyncCallback to get some dynamic data. If the call is successfull (onSuccess), the data is written into the listbox.

The GreetingService interface define the synchronous methods, it is implemented in the GreetingServiceImpl class :

@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String[] getSomeEntries() ;
}

The asynchronous counterpart is the GreetingServiceAsync interface, we used it before to call the server :

public interface GreetingServiceAsync {
void  getSomeEntries(AsyncCallback<String[]> callback) ;
}

The GreetingServiceImpl class is located on the server. Here you could call for example a database:

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService {

@Override
public String[] getSomeEntries() {
    String[] entries = { "Entry 1","Entry 2","Entry 3" };
    return entries;
}


    }

Now if you want to use some Bean/Pojo between the server and client, replace the String[] in each class/interface with the object name, put the class in the shared package and consider that it implements Serializable/IsSerializable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top