GWT Visualization API DataTable Serialization

I am trying to complete this tutorial on how to connect to a database in GWT, but instead of creating a login program, I am trying to get a GWT Visulation DataTable from my database, then create Annotated TimeLine. I went very far, but I hit the last wall, I can’t understand. Unlike tut, I do not return a simple User class from RPC, but a complex DataTable. The problem is that this DataTable must be serializable by GWT standards. Is there an easy way to do this?

I use RPC instead of Query for security reasons. I don't want people to be able to look at my javascript and see my requests, etc.

Thanks.

UPDATE: Returning to the problem, I found that a DataTable is a JavaScriptObject and probably should never have been done on the server side. So, a new question. What is the best way to manually make a DataTable something cermeable, and then the best way to redo it on the client side. Thanks again!

+5
source share
3 answers

Ok, so I figured it out myself (sorta), so I decided that I would post the answer here if someone else would have the same problem later.

, . DataTable - JSO, GWT (1,6) . , , ArrayLists Temperay. . , DataTable .

- , .

.

-

+4

,

String json = JsonRenderer.renderDataTable(yourDataTable, true, true);

-

public static native DataTable toDataTable(String json) /*-{
  return new $wnd.google.visualization.DataTable(eval("(" + json + ")"));
}-*/;

,

+1

API Google Google Web Toolkit (, gwt-visualization.jar) ( javascript). , Google Java DataTables Google Source Data Source Library.

, DataTables , JSON Google Google Web Toolkit, Google Plot -. Eclipse Indigo Google Web Toolkit 2.4.0.

src/com.package.name/project-name.xml:

<inherits name='com.google.gwt.visualization.Visualization'/>
  • Google PROJECT/war/WEB-INF/lib
  • , :

client/TableService.java:

package com.clark.demos.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("table")
public interface TableService extends RemoteService {
    String getTable(); 
}

/TableServiceAsync.java:

package com.clark.demos.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface TableServiceAsync {
    void getTable( AsyncCallback<String> callback );
}

/WEB -INF/web.xml:

  <servlet>
    <servlet-name>tableServlet</servlet-name>
    <servlet-class>com.clark.demos.server.TableServiceImpl</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>tableServlet</servlet-name>
    <url-pattern>/google_visualization___gwt/table</url-pattern>
  </servlet-mapping>
  • "table" :

/TableServiceImpl.java:

package com.clark.demos.server;

import com.google.visualization.datasource.datatable.ColumnDescription;
import com.google.visualization.datasource.datatable.DataTable;
import com.google.visualization.datasource.datatable.value.ValueType;
import com.google.visualization.datasource.render.JsonRenderer;

@SuppressWarnings("serial")
public class TableServiceImpl extends RemoteServiceServlet implements
        TableService {

    @Override
    public String getTable() {      
        DataTable data = new DataTable();
        data.addColumn( new ColumnDescription("Task", ValueType.TEXT, "Task") );
        data.addColumn( new ColumnDescription("Stemming", ValueType.NUMBER, "Stemming") );
        data.addColumn( new ColumnDescription("NoStemming", ValueType.NUMBER, "No Stemming") );
        data.addRowFromValues( "Fire", 1.0, 0.8 );
        data.addRowFromValues( "Flood", 0.5, 0.65 );            
        return JsonRenderer.renderDataTable(data, true, false, false).toString();
        }

}
  • "table" DataTable JSON:

client/gwt-visualization-demo.java:

/**
 * Create a remote service proxy to talk to the server-side Table service.
 */
private final TableServiceAsync tableService = GWT
        .create(TableService.class);  

public static native DataTable toDataTable(String json) /*-{
  return new $wnd.google.visualization.DataTable(eval("(" + json + ")"));
}-*/;   

public void onModuleLoad() {

// Create a callback to be called when the visualization API
// has been loaded.
Runnable onLoadCallback = new Runnable() {
    public void run() {
        final Panel panel = RootPanel.get();

        tableService.getTable(new AsyncCallback<String>() {

            @Override
            public void onSuccess(String result) {                  
                AbstractDataTable data = toDataTable(result);
                BarChart pie = new BarChart(data, createOptions());

                pie.addSelectHandler(createSelectHandler(pie));
                panel.add(pie);             
            }

            @Override
            public void onFailure(Throwable caught) {               
            }
        });
      }
    };

// Load the visualization api, passing the onLoadCallback to be called
// when loading is done.
VisualizationUtils.loadVisualizationApi(onLoadCallback, BarChart.PACKAGE);

}

, https://github.com/RichDickClark/gwt-google-charts-demo.git

+1

All Articles