markzzz
markzzz

Reputation: 47945

GWT - Is it possible to create new HTML elements (from the server) or i can just to update the ones loaded on the client?

Im new about this technology, but I would like to know if is possible to create new object (html elements, such div/span/and so on...) dinamically on server and send it to the client, or if i can just load the ones made on client-side when i develop it in the application.

I don't ask how to do it (i think its a delicate argument), but if I can, and (if yes) where i can get some stuff/example/tutorial to do this.

Example

What i usually do :

...
public void onSuccess(Boolean result) {
    if(result) {
        myFunction();
    }
}

...

myFunction() {
    InlineLabel label=new  InlineLabel();
    this.add(label)    
}

What im looking for :

...
public void onSuccess(InlineLabel result) {
    this.add(result) 
}

So, i don't need to load in advance the Object, but load them only if i click on some button (or if i perform an action). This will save a lot of code (that is inutil, if i don't do any action) loaded (as JavaScript) on the client.

As usual, thanks for your time!

Upvotes: 0

Views: 3662

Answers (2)

Riley Lark
Riley Lark

Reputation: 20890

GWT does not support the pattern you showed, but you can achieve a similar effect with "code splitting": read http://code.google.com/webtoolkit/doc/latest/DevGuideCodeSplitting.html

With code splitting, the client only downloads the script it needs right away (configured by the developer). If, for example, the user navigates to a more complex area of the UI that requires more widgets, additional code will be downloaded.

Upvotes: 2

Wesley
Wesley

Reputation: 10852

I'm not entirely sure I understand your question, but please feel free to amend your question or post a comment if I've missed the mark.

The host page

A GWT app is loaded in the following (simplified) process:

  • A host page (HTML) is loaded
  • A bootstrapping script is loaded
  • A compiled app script is loaded

The host page can contain any HTML you want. The only requirement is that you include a <script> element that loads the GWT bootstrapping script.

As a result, you can have the server return a page that contains any server-generated markup you like.

Server-rendered HTML at runtime

Once your app is running, you can send off asynchronous requests in your code to retrieve arbitrary data from the server. One option is to retrieve server-generated HTML and insert it into your application.

For this option, you'll want to instantiate an HTML widget, then use its setHTML method to insert the server-generated markup into the widget.

Client-generated

As an alternative, you can retrieve structured data from the server via GWT RPC. Objects created on a Java-based server are serialised by GWT on the server and deserialised on the client back into regular objects. You can then pull data out of these objects using accessor methods (getName, getId, etc.). At this point, you have several options:

  • Generate some HTML using StringBuilder and the like, then use setHTML on an HTML widget.
  • Generate DOM elements with the DOM class
  • Set the data into widgets and add them to panels or the root panel.

Upvotes: 1

Related Questions