Vaadin provide a Navigator to navigate between pages, but I don't like the way it writes the url, for example /VaadinApp/#!login. How can I change that URI to /VaadinApp/login. Have anyone tried to do that ?

有帮助吗?

解决方案

You need extend UriFragmentManager and override getState() and setState() methods:

public void setState(String state)  {
   setFragment(state);
}

public String getState() {
   return getFragment() != null ? getFragment() : "";
}

其他提示

Hold on a second before you write a custom UriFragmentManager. Are you sure, you want to use URIs like /VaadinApp/login? If you only want to get rid of the ! in your URI fragments, you can follow Alexey's proposal and write a custom UriFragmentManager. But there is a good reason why Vaadin uses the shebang #! for URI fragments (look here).

If you really don't want to use the hash mark, think twice. This would basically mean to go without URI fragments at all, which is not a road you'll want to travel with a single-page application framework like Vaadin. A Vaadin app runs on a single page and any user action and navigation is handled through lightweight Ajax requests. If you use a URL structure like /VaadinApp/login, /VaadinApp/profile, /VaadinApp/main etc., your Vaadin app would end up doing a full reload after each navigation step made by the user. This would result in an unacceptable flickering effect, since reloading a Vaadin app would mean a full rerendering of the page contents by the browser through the comparatively slow JavaScript engine. Make sure you reread the section about the Navigator component in the Book of Vaadin. Besides, even if not recommended, this solution is not possible to achieve by using a custom UriFragmentManager.

That said, if you want to implement Alexey's solution you write a subclass of com.vaadin.navigator.Navigator.UriFragmentManager with the two methodes getState() and setState() as shown above (this proposed implementation simply forgoes the exclamation mark in contrast to the original implementation). Then you pass an instance of this custom fragment manager to the following one of Navigator's constructors:

Navigator(UI ui, NavigationStateManager stateManager, ViewDisplay display)

UriFragmentManager implements the NavigationStateManager interface.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top