Spring(-Boot) & Vaadin - Set default-view in Vaadin navigator

风格不统一 提交于 2019-12-24 03:50:54

问题


first of all I have here a simple UI which has a addMenueItem() Method. This method gets a view-id and adds a button to the menue and tells the navigator from Vaadin to navigate to it on click:

.........
    @PostConstruct
    private void initPage() {
        navigator.addProvider(viewProvider);
        contentLayout.setSizeFull();
    }

    protected void addMenuItem(final String viewId) {
        final String postfix;
        if (viewId == null || viewId.trim().isEmpty()) {
            postfix = "";
        } else {
            postfix = "." + viewId;
        }
        final String name = messageByLocaleService.getMessage(I18N_PREFIX + postfix);
        menue.addItem(name, menueCommand -> getUI().getNavigator().navigateTo(viewId));
    }
.........

So I'm adding some views from my MainUI like:

addMenuItem(DefaultView.VIEW_ID);

The Spring url based view resolver calls the now the id from the value of 'DefaultView.VIEW_ID'. Now I'm looking for a good solution to map a given view-id not only to ' /VIEW-ID ' but also to the application root path --> ' / '.

How can I tell Spring that this special ViewID is also the root or an synonym for /VIEW-ID ?

For sure there is a possibility to hard-code this in some kind of xml-file of Tomcat or something, but i would like to do so dynamically.

Thanks in advance

PS: I'm very new to spring and this kind of stuff, courtesy please :D


回答1:


The navigator and the URI fragment go hand in hand. The root path does not have any URI fragment therefore the navigator does not know where to navigate when the URL is called.

You can solve that problem in your UI class like this:

@SpringUI
public class MainUI extends UI {
    @Autowired
    SpringViewProvider viewProvider;

    @Override
    protected void init(VaadinRequest request){
        Navigator navigator = new Navigator(this,this);
        navigator.addProvider(viewProvider);
        this.setNavigator(navigator);

        // Set default view
        NavigationStateManager stateManager = new Navigator.UriFragmentManager(getPage());
        stateManager.setState(DefaultView.VIEW_ID);
    }
}

Explanation:

Most of the stuff should be well known already because these are basics that can be read in several tutorials, e.g.: III Views and Navigation with Vaadin Spring

In short:

  • You use the @SpringUI annotation, which by default maps to /, to register a new UI
  • You autowire the SpringViewProvider which automatically registers your views.
  • You initialize the navigator.

To set the default view you simply initialize the NavigationStateManager which sets the URI fragment. Once the init method completes the UI class calles the Navigator.navigateTo() method with the current state of the NavigationStateManager.

NOTE: You might find something in the net like:

navigator.navigateTo(DefaultView.VIEW_ID);

If you use this in the init method of the UI your navigator is called twice.



来源:https://stackoverflow.com/questions/33059365/spring-boot-vaadin-set-default-view-in-vaadin-navigator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!