マイケル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(""));