Bita Mirshafiee
Bita Mirshafiee

Reputation: 650

GWT save data in TextBox by clicking a Button

I have a simple application containing a TextBox and Button by using GWT, the point is that when I write something in the TextBox and click the Button the data in the TextBox would be saved in database, here is what I wrote, but I don't know how to save it by clicking:

    button.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {

            //method to save the data

        }
    });

Upvotes: 0

Views: 551

Answers (3)

Manjul
Manjul

Reputation: 21

If you are asking how to connect GWT to your server database, this link might be helpful.

XML: The bridge between GWT and PHP

Simply take the input value from textbox and post it to your server

Upvotes: 0

moh
moh

Reputation: 1526

Please elaborate your question..I understood like how to save value in database from text box if it so then ,

  1. Get the Value from Text Box
  2. Send the Value to Server either by using any one of the following approach

    • RPC
    • RequestBuilder
  3. Push the Data(value) to database

Example:

services.saveData(textValue, new AsyncCallback<Response>() {
     public void onSuccess(Response response) {
         showMessage("Saved Successfully");
     }

     public void onError(Throwable error) {
         showError("Failed to Save Data");
     }
};

Upvotes: 0

thst
thst

Reputation: 4602

You need to implement a remote service to be used by your application.

GWT uses RemoteService to do server tasks. A remote service is an interface defining your remote methods, it must be placed in the shared classpath of your application. The remote service interface is implemented by the servercode to execute the actual action you want it to perform. The client uses a so called Async interface, that is in the client class path and ususally generated for you by the IDE or a special compiler target. In mvn projects, you will find them in the target/generated-sources folder. Those async interfaces that are implemented by the compiler and instantiated by your code using GWT.create().

The async interface has the same methods as the remote service interface, but all methods have a changed signature. Instead of a return value, they expose an AsyncCallback<T> handler, that is used to call your code after the remote call on the server returned. <T> is the return type of your method in the RemoteService interface on the server.

The proper way to save your data, is to have a presenter class or activity class that will have an instance of the async service. The activity/presenter will take the value from the view and send it to the remote service for storing into database.

In your click handler, you use the activity to save the data, or the presenter attaches himself as the click handler to the view.

There is plenty of code samples showing the principle in gwtproject.org tutorials section.

The call in the presenter/activity will use an async callback class like so:

backendSvc.saveData( textString, new AsyncCallback<Response>() {
     void onSuccess(Response r) {
         view.showMessage("Response saved");
     }

     void onError(Throwable caught) {
         view.showError("Ouch", caught);
     }
}

The view should not call the backend directly, this will lead to a mess, if your application grows.

I suggest, you check tutorials on the gwtproject website and really try to understand the flow of events. The concept is fundamental to understand the GWT way.

Upvotes: 1

Related Questions