Ali Abbas
Ali Abbas

Reputation: 488

How to handle multiple pages in gwt application

I am working on gwt application I am using "Anchor" for this , I exactly want that without page refresh my content should be change and also browser will update the url

Upvotes: 0

Views: 124

Answers (2)

Tobika
Tobika

Reputation: 1062

Activies and Places needs a lot of boilerplate.

If you just want to update the URL you should use the the GWT History Mechanism. CodeExample:

public App implements ValueChangeHandler<String>{
    // listen for the History Change Events
    History.addValueChangeHandler(this);

    private void showPanelOne(){
        container.clear();
        container.add(new Panel());
    }

    private void showPanelTwo(){
        container.clear();
        container.add(new Panel());
    }

    public void onValueChange(ValueChangeEvent<String> event) {
        String token = event.getValue();   
        if (token != null) {
            if (token.contains(Tokens.PANELONE)) {
                showPanelOne()
            } else if (token.contains(Tokens.PANELTWO)) {
                showPanelTwo()
            }
        } else {
            //show default panel or just do nothing?
        }
    }
}  

public class Tokens {
    public final static String PANELONE = "!/panelone";
    public final static String PANELTWO = "!/paneltwo";
}

To change the URL just

1)call:

History.newItem(Tokens.PANELTWO)

2)use HTML in UI binder:

<a href="#{Tokens.PANELONE}">Show PanelOne</a>

This will change the part after the # what will look like http://test.com/#!/panelone. The onValueChange method registers that the URL is updated and handles what happens then, for example show another window. Hashbang(#!) is used to make the site seo friendly (see Better SEO with GWT )

An example source code of an seo friendly homepage that updates the URL can you find here.

EDIT: seo stuff and example added

Upvotes: 0

enrybo
enrybo

Reputation: 1785

You probably want to use Activities and Places

Basically the framework lets you have Places which you can navigate to with the PlaceController. This does not trigger a full page reload but will change the current view being shown.

In fact the Place calls the matching Activity which is the "brains" of the page. The activity can then add the View to the page. The View contains as little logic as possible and is suppose to hold all of the user interface. The View should in fact be an interface which a ViemImplementation can implement therefore you can have mane Views for the same Activity (eg desktop, mobile, tablet).

Upvotes: 1

Related Questions