Spring(-Boot) &Vaadin - 在 Vaadin 导航器中设置默认视图 [英] Spring(-Boot) & Vaadin - Set default-view in Vaadin navigator

查看:48
本文介绍了Spring(-Boot) &Vaadin - 在 Vaadin 导航器中设置默认视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我这里有一个简单的 UI,它有一个 addMenueItem() 方法.此方法获取一个视图 ID 并向菜单添加一个按钮,并告诉 Vaadin 的导航器在单击时导航到它:

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));
    }
.........

所以我从我的 MainUI 中添加了一些视图,例如:

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

addMenuItem(DefaultView.VIEW_ID);

基于 Spring url 的视图解析器从DefaultView.VIEW_ID"的值中调用现在的 id.现在我正在寻找一个很好的解决方案来将给定的视图 ID 不仅映射到"/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 --> ' / '.

我如何告诉 Spring 这个特殊的 ViewID 也是/VIEW-ID 的根或同义词?

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

当然有可能在某种 Tomcat 的 xml 文件或其他东西中对此进行硬编码,但我想动态地这样做.

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.

提前致谢

PS:我对春天和这种东西很陌生,请礼貌:D

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

推荐答案

导航器和 URI 片段齐头并进.根路径没有任何 URI 片段,因此导航器在调用 URL 时不知道要导航到哪里.

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.

你可以在你的 UI 类中解决这个问题:

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);
    }
}

说明:

大部分内容应该是众所周知的,因为这些是可以在几个教程中阅读的基础知识,例如:Vaadin Spring 的 III 视图和导航

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

简而言之:

  • 您使用 @SpringUI 注释(默认映射到 /)来注册新 UI
  • 您自动装配 SpringViewProvider,它会自动注册您的视图.
  • 您初始化导航器.
  • 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.

要设置默认视图,您只需初始化设置 URI 片段的 NavigationStateManager.init 方法完成后,UI 类将使用 NavigationStateManager 的当前状态调用 Navigator.navigateTo() 方法.

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);

如果您在 UI 的 init 方法中使用它,您的导航器将被调用两次.

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

这篇关于Spring(-Boot) &Vaadin - 在 Vaadin 导航器中设置默认视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆