This tutorial will help you to build a simple page using the Rialto GWT API. For the exemple we will create a simple login/password page. And the button click will check the login/password on the server.
Refer to the get start tutorial
We are going to create the following components
package fr.improve.gwt.client; import java.util.List; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.ServiceDefTarget; import com.google.gwt.user.client.ui.RootPanel; import fr.improve.gwt.client.service.LogonService; import fr.improve.gwt.client.service.LogonServiceAsync; import fr.improve.gwt.client.service.ReloadNodeService; import fr.improve.gwt.client.service.ReloadNodeServiceAsync; import fr.improve.rialto.gwt.core.client.Alert; import fr.improve.rialto.gwt.core.client.Button; import fr.improve.rialto.gwt.core.client.Frame; import fr.improve.rialto.gwt.core.client.GridProvider; import fr.improve.rialto.gwt.core.client.GridTreeProvider; import fr.improve.rialto.gwt.core.client.Label; import fr.improve.rialto.gwt.core.client.Password; import fr.improve.rialto.gwt.core.client.RialtoObject; import fr.improve.rialto.gwt.core.client.SimpleWindow; import fr.improve.rialto.gwt.core.client.Text; import fr.improve.rialto.gwt.core.client.TreeProvider; import fr.improve.rialto.gwt.core.client.event.ClickListener; import fr.improve.rialto.gwt.core.client.parameter.FrameParameter; public class TestGWT implements EntryPoint { private SimpleWindow window; private Text username; private Password password; public void onModuleLoad() { Element body = RootPanel.getBodyElement(); window = SimpleWindow.create("TEST", 0, 0, 800, 400, body, false); FrameParameter frmParam = FrameParameter.create(); frmParam.setName("Frame"); frmParam.setPosition("relative"); frmParam.setDraggable(true); frmParam.setDynamic(true); frmParam.setOpen(true); frmParam.setTop(0); frmParam.setLeft(0); frmParam.setWidth("460"); frmParam.setHeight(280); frmParam.setTitle("Login/Password"); frmParam.setRialtoParent(window); Frame frame = Frame.create(frmParam); Label.create("name", 45, 10, frame, "Username"); username = Text.create("nameText", 40, 130, 200, frame); Label.create("password", 75,10, frame, "Password"); password = Password.create("passwordText", 70, 130, 200, frame); Button button1 = Button.create(115, 80, "Logon", null, 100, frame); Button button2 = Button.create(115, 200, "Cancel", null, 100, frame); } }
So, it is necessary to send the login and the password to the server for check. GWT proposes for it an API implementing the call of services via asynchronous callbacks.
The first stage of the realization of this service consists in creating one interface for our service. For example, the following interface defines a methode checkLogin, taking in parameter a login, a password and returning a boolean if the couple matches. A good practice is to place this code in one package ending in “service” (in client necessarily).
package fr.improve.gwt.client.service; import com.google.gwt.user.client.rpc.RemoteService; public interface LogonService extends RemoteService { Boolean checkLogin(String username, String password); }
We also have to define the asynchronous service. The asynchronous service is defined by one interface almost identical to the service, but without type of return and with a supplementary parameter AsyncCallBack.
package fr.improve.gwt.client.service; import com.google.gwt.user.client.rpc.AsyncCallback; public interface LogonServiceAsync { void checkLogin(String username, String password, AsyncCallback callback); }
Our interface must be implemented by a special server side servlet. Here is an example of implementation. A good practice is to place this code in one package that end with ” server ”. Note that the service inherits from RemoteServiceServlet and implements LogonService.
package fr.improve.gwt.server; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import fr.improve.gwt.client.service.LogonService; public class LogonServiceImpl extends RemoteServiceServlet implements LogonService { public Boolean checkLogin(String username, String password) { //Do what you want to check thelogin password return new Boolean(true); } }
To call our service since our screen, it is necessary to pass by GWT.create ( Class) to get back the service. Here is an example where the service is call in the button listener after retrieve login and password value/
button1.setClickListener(new ClickListener() { public void onClick(RialtoObject sender) { String login = username.getValue(); String pass = password.getValue(); LogonServiceAsync logonService = (LogonServiceAsync) GWT.create(LogonService.class); ServiceDefTarget endpoint = (ServiceDefTarget) logonService; String moduleRelativeURL = GWT.getModuleBaseURL() + "logon"; endpoint.setServiceEntryPoint(moduleRelativeURL); AsyncCallback callback = new AsyncCallback() { public void onFailure(Throwable caught) { // TODO } public void onSuccess(Object result) { Boolean ok = (Boolean) result; if (ok.booleanValue()) { //LOGIN OK Alert.alert("LOGIN OK"); } else { Alert.alert("Invalid login/password !"); } } }; logonService.checkLogin(login, pass, callback); } });
In production, the service must be deploy as a standard servlet, with the name specified during its use (here ” logon ”). In development, it is possible to configure GWT so that the servlet is available in the environment GWT by adding a line in the file of configuration of the GWT module:
<module> <!-- ... --> <servlet path="/logon" class="fr.improve.gwt.server.LogonServiceImpl"/> </module>