Ce contenu n'est pas disponible dans la langue sélectionnée.

25.3. Hooking up a GWT widget to the Seam component


Next, write a method that returns the asynchronous interface to the component. This method can be located inside the widget class, and will be used by the widget to obtain a reference to the asynchronous client stub:
private MyServiceAsync getService() {       
  String endpointURL = GWT.getModuleBaseURL() + "seam/resource/gwt";      
      
  MyServiceAsync svc = (MyServiceAsync) GWT.create(MyService.class);
  ((ServiceDefTarget) svc).setServiceEntryPoint(endpointURL);
  return svc;     
}
Copy to Clipboard Toggle word wrap
Finally, write the widget code that invokes the method on the client stub. The following example creates a simple user interface with a label, text input, and a button:
public class AskQuestionWidget extends Composite {
  private AbsolutePanel panel = new AbsolutePanel();
   
  public AskQuestionWidget() {      
    Label lbl = new Label("OK, what do you want to know?");
    panel.add(lbl);
    final TextBox box = new TextBox();
    box.setText("What is the meaning of life?");
    panel.add(box);
    Button ok = new Button("Ask");
    
    ok.addClickListener(new ClickListener() {
    
      public void onClick(Widget w) {
        ValidationUtility valid = new ValidationUtility();
        if (!valid.isValid(box.getText())) {
          Window.alert("A question has to end with a '?'");
        } else {
          askServer(box.getText());
        } 
      }
    });
    panel.add(ok);
      
    initWidget(panel);
  }

  private void askServer(String text) {
    getService().askIt(text, new AsyncCallback() {
    public void onFailure(Throwable t) {
      Window.alert(t.getMessage());
    }

    public void onSuccess(Object data) {
      Window.alert((String) data);
    }         
  });      
}
   
...
Copy to Clipboard Toggle word wrap
When clicked, this button invokes the askServer() method, passing the contents of the input text. In this example, it also validates that the input is a valid question. The askServer() method acquires a reference to the asynchronous client stub (returned by the getService() method) and invokes the askIt() method. The result (or error message, if the call fails) is shown in an alert window.
The complete code for this example can be found in the Seam distribution in the examples/remoting/gwt directory.
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat