Je suis en train de faire les premiers pas aujourd'hui sur le framework GWT. Je dois comprendre (en utilisant le NetBeans officiel tutorial comment ce travail d'application :) Je place le code:GWT - Principales étapes pour ce code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.yournamehere.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* Main entry point.
*
* @author djfonplaz
*/
public class MainEntryPoint implements EntryPoint {
/**
* Creates a new instance of MainEntryPoint
*/
public MainEntryPoint() {
}
public static GWTServiceAsync getService() {
// Create the client proxy. Note that although you are creating the
// service interface proper, you cast the result to the asynchronous
// version of the interface. The cast is always safe because the
// generated proxy implements the asynchronous interface automatically.
return GWT.create(GWTService.class);
}
public void onModuleLoad() {
final Label quoteText = new Label();
Timer timer = new Timer() {
public void run() {
//create an async callback to handle the result:
AsyncCallback callback = new AsyncCallback() {
public void onFailure(Throwable arg0) {
//display error text if we can't get the quote:
quoteText.setText("Failed to get a quote");
}
public void onSuccess(Object result) {
//display the retrieved quote in the label:
quoteText.setText((String) result);
}
};
getService().myMethod(callback);
}
};
timer.scheduleRepeating(1000);
RootPanel.get().add(quoteText);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.yournamehere.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
/**
*
* @author djfonplaz
*/
@RemoteServiceRelativePath("gwtservice")
public interface GWTService extends RemoteService {
public String myMethod();
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.yournamehere.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
*
* @author djfonplaz
*/
public interface GWTServiceAsync {
public void myMethod(AsyncCallback callback);
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.yournamehere.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.yournamehere.client.GWTService;
/**
*
* @author djfonplaz
*/
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
private Random randomizer = new Random();
private static final long serialVersionUID = -15020842597334403L;
private static List quotes = new ArrayList();
static {
quotes.add("No great thing is created suddenly - Epictetus");
quotes.add("Well done is better than well said - Ben Franklin");
quotes.add("No wind favors he who has no destined port - Montaigne");
quotes.add("Sometimes even to live is an act of courage - Seneca");
quotes.add("Know thyself - Socrates");
}
public String myMethod() {
return (String) quotes.get(randomizer.nextInt(5));
}
}
So (plus ou moins):
- l'welcomeGWT.html standard est servi à la serveur, qui appelait directement le JS créé le servlet MainEntryProject.java
- MainEntryProject.java (quand est chargé chargé onModuleLoad()) qui devrait générer la chaîne et envoyer au client.
À ce stade?
Ce que je ne comprends pas:
- Qui appelle la méthode myMethod() dans GWTServiceImpl? Personne ne demande cette méthode, je vois juste getService(). MyMethod (callback), qui devrait appeler celui de la classe GWTServiceAsync.
- Qui transmet la chaîne générée par GWTServiceImpls à public void onSuccess (Résultat de l'objet)?
- Pourquoi getService() renvoie GWTService et non GWTServiceImpl? Il devrait retourner une classe, pas une interface;
Si quelqu'un peut m'aider, je serais vraiment heureux! acclamations
Uhm ... ok! Je trouve juste qu'il bizzare hehe (en fait je ne connais pas ce cadre du tout je pense hehe). Ainsi, appeler la méthode "abstract" de GWTServiceAsync appellera automatiquement l'implémentation de cette méthode écrite sur GWTServiceImpl? Après, je peux gérer ce que cette méthode retourne en utilisant l'instance "résultat"? – markzzz
Oui, si vous appelez la méthode à partir du service YourServiceAsync, cela entraînera l'invocation de la méthode du serveur, à partir du fichier YourServiceImpl. Pensez que cet article devrait expliquer tout le reste: http://code.google.com/intl/fr/webtoolkit/doc/1.6/DevGuideServerCommunication.html#DevGuideRemoteProcedureCalls –
Et le type de retour défini dans l'interface YourService comme d'habitude, YourServiceAsync dans l'argument AsyncCallback comme : "Rappel AsyncCallback". Et puis côté serveur dans la méthode YourServiceImpl: "public String myMethod (String s) {...}" –