別のビュー火災ことComponentEventにMAINVIEWを登録するには?

マイケルKemmerzell:

どのように私は別のビュー火災そのイベントにMAINVIEWを登録できますか?

私が試してみましたComponentUtil.addListenerを()が、それは私のために動作しません。一方で私もわからビューは、火災がイベントがさえ時点で作成されていることをMAINVIEWは、イベントに登録することができないのですか?

アプリケーション・フロー:

開いているアプリケーション- > MainView- >ユーザーが認証されていない場合は、リルート- > LoginView- > SuccessfulAuthenticationEvent- > MainViewに反応してSuccessfulAuthenticationEventで解雇しましたLoginView

イベント:

public class SuccessfulAuthenticationEvent extends ComponentEvent<LoginForm> {
    public SuccessfulAuthenticationEvent(LoginForm source, boolean fromClient) {
        super(source, fromClient);
    }
}

MAINVIEW:

public class MainView extends AppLayout implements ComponentEventListener<SuccessfulAuthenticationEvent> {
    ...
    @Override
    public void onComponentEvent(SuccessfulAuthenticationEvent event) {
        System.out.println("SuccessfulAuthenticationEvent catched");
    }
}

LoginView:

public class LoginView extends Div {
    ...
    @Override
    public void executeAfterAuthentication() {
        fireEvent(new SuccessfulAuthenticationEvent(loginForm, false));
    }
}
タトゥルンド:

私は、より堅牢である別のアプローチを、使用します。

ログインビューで。ログインに成功すると、セッション属性保存ユーザーがログインしたことを示す、例えば

VaadinSession.getCurrent().setAttribute("userLoggedIn", true);

あなたのMainLayoutで実装BeforeEnterObserverし、オーバーライドbeforeEnterメソッド。beforeEnter、それは前方ログインページにnullの場合メソッド、セッション属性をお読みください。

@Override
public void beforeEnter(BeforeEnterEvent event) {
    if (VaadinSession.getCurrent().getAttribute("userLoggedIn") == null) {
        event.forwardTo(LoginView.class);
    }
}

エクストラ先端。ではbeforeEnterログインが保存されたルートに成功し、リルートの場合はこの方法、あなたはまた、現在のルートを保存し、ログインビューですることができます。

@Override
public void beforeEnter(BeforeEnterEvent event) {
    if (VaadinSession.getCurrent().getAttribute("userLoggedIn") == null) {
        VaadinSession.getCurrent().setAttribute("intendedPath", event.getLocation().getPath());
        ...
    }
}

そして、ログインに成功したログイン後に表示

Object intendedPath = VaadinSession.getCurrent().getAttribute("intendedPath");
UI.getCurrent().navigate(Optional.ofNullable(intendedPath).map(Object::toString).orElse(""));

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=296665&siteId=1