自定义Vaadin Navigator

时间:2016-11-24 08:58:17

标签: java spring vaadin

我刚刚扩展了Vaadin的导航器,并覆盖了一些方法:

public class MyNavigator extends Navigator {

final SpringViewProvider viewProvider;

public MyNavigator(UI ui, SingleComponentContainer container, SpringViewProvider viewProvider) {
    super(ui, container);
    this.viewProvider = viewProvider;
    addProvider(this.viewProvider);
}

@Override
public void navigateTo(String navigationState) {
    if(ViewTokens.AVAILABLE_TOKENS.contains(navigationState)) {
        if(viewProvider.getView(navigationState) == null) {
            String uriFragment = parseViewToken();
            if(uriFragment.equals(navigationState)) {
                super.navigateTo(ViewTokens.HOME);
            }
        } else {
            //if nothing found, redirect
            super.navigateTo(ViewTokens.HOME);
        }
    }
}

private String parseViewToken() {
   <ommitted>
}

}

这就是我设置Navigator的方法:

@SpringUI(path="/")
@Theme(ValoTheme.THEME_NAME)
public class MyFancyUI extends UI {

@Autowired
SpringViewProvider viewProvider;

@Override
protected void init(VaadinRequest vaadinRequest) {
    MyNavigator navigator = new MyNavigator(UI.getCurrent(), this, viewProvider);
    setNavigator(navigator);
    navigator.navigateTo(ViewTokens.SIGNIN);
}

}

这就是我试图获取导航器并将视图重定向到另一个视图的方式:

@UIScope
@SpringView(name=ViewTokens.HOME)
public class SplashScreen extends VerticalLayout implements View {
private ComponentHelper componentHelper;
private Button toLoginPage;

public SplashScreen() {
    initComponents();
    buildSplashView();
}

protected void initComponents() {
    componentHelper = ComponentHelper.getInstance();
    toLoginPage = componentHelper.createFriendlyButton("To Login Page",220, 60);
    toLoginPage.addClickListener(this::redirect);
}

private void buildSplashView() {
    addComponent(toLoginPage);
    setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
    setHeight(100, Unit.PERCENTAGE);
}

public void redirect(Button.ClickEvent event) {
    getUI().getNavigator().navigateTo(ViewTokens.SIGNIN);
}

@Override
public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent){

} 
}

由于某种原因,按钮不会对点击事件做出反应。

更换为标准导航器 - 它可以工作。

我做错了什么?

感谢您的建议

1 个答案:

答案 0 :(得分:1)

我原本以为你可以使用Vaadin的Navigator和这样的View Change Listener:

        navigator.addViewChangeListener(new ViewChangeListener() {

        @Override
        public boolean beforeViewChange(ViewChangeEvent event) {
            // check you're happy to change

            if (ViewTokens.AVAILABLE_TOKENS.contains(navigationState)) {
                if (viewProvider.getView(event.getViewName()) == null) {
                    String uriFragment = parseViewToken();
                    if (uriFragment.equals(event.getViewName())) {
                        navigator.navigateTo(ViewTokens.HOME);
                        return false;
                    }
                } else {
                    // if nothing found, redirect
                    navigator.navigateTo(ViewTokens.HOME);
                    return false;
                }
                // Let the view change happen.
                return true;
            }

        }

        @Override
        public void afterViewChange(ViewChangeEvent event) {

        }
    });

如果你在beforeViewChange中返回false,它将停止发生视图更改。