GWT. Set URL without submit

Can I change the url (set the parameter) without sending? I found this method http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/Window.Location.html#replace%28java.lang.String%29 but he represents the page. All GWT status will be lost.

+6
gwt
source share
3 answers

Why are you trying to do this? Generally speaking, GWT applications do not change pages - so they are usually SPA (single page applications)

When you load a new page from the server, you will lose state on this page. You can change the hash of the url as it will not return to the server, for example:

String newURL = Window.Location.createUrlBuilder().setHash("newhash").buildString(); Window.Location.replace(newURL); 

However, if you intend to do this, I would recommend taking a look at the GWT MVP framework, which has built support for managing locations using hash tokens.

http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html

+7
source share

If you want to change something not in the hash, for example, you want to change the parameter in the URL, you can do it like this:

 private void someMethod() { String newURL = Window.Location.createUrlBuilder().setParameter("someParam", "someValue").buildString(); updateURLWithoutReloading(newURL); } private static native void updateURLWithoutReloading(String newUrl) /*-{ $wnd.history.pushState(newUrl, "", newUrl); }-*/; 

Then you can register a function that processes the user using the back and forward buttons of the browser, as shown here .

+9
source share
  $wnd.history.pushState(newUrl, "", newUrl); 

Works well in HTML5 browsers. Not in IE8 or IE9!

+2
source share

All Articles