简体   繁体   English

vaadin viewchangelistener更改newView

[英]vaadin viewchangelistener change newView

I try to change the newView of a navigation if the user is not authenticated and the newView is an instance of SecuredView . 如果用户未通过身份验证并且newView是SecuredView的实例, SecuredView尝试更改导航的SecuredView My approach was to check inside the beforeViewChange method if the constraints are given. 我的方法是检查beforeViewChange方法内部是否给出了约束。

public boolean beforeViewChange(ViewChangeEvent event) {
    if (event.getNewView().getClass().isInstance(SecuredView.class) && VaadinService.getCurrentRequest().getUserPrincipal() == null) {
        event.getNavigator().navigateTo("login");
        return false;
    }
        return true;
    }
}

But that will result in a endless loop of redirection... How can i change the newView and stop the current navigation event? 但这将导致无休止的重定向循环...如何更改newView并停止当前的导航事件?

1) If your View is not 'a correct view' it will end up in an endless loop. 1)如果您的View不是“正确的视图”,它将以无休止的循环结束。 Try this simple view: 试试这个简单的视图:

public class SecuredView extends VerticalLayout implements View {

    public SecureView() {
        addComponent(new Label("Secured View"));
    }

    @Override
    public void enter(ViewChangeEvent event) {
    }

}

2) Your if statement needs to be changed: 2)您的if语句需要更改:

instead of: 代替:

event.getNewView().getClass().isInstance(SecuredView.class)

write: 写:

SecuredView.class.isInstance(event.getNewView());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM