2

On a click on a Button, I need to do some action and then redirect to an external url.

All example I find are for older Vaadin version, and doesn't work on Vaadin 10.

Can someone provide an example please ?

Tyvain
  • 2,640
  • 6
  • 36
  • 70

1 Answers1

2

In most cases I would recommend you to use the new Anchor component in Vaadin 10+. Its purpose is to cover your use case, replace BrowserWindowOpener, etc.

If your use case is to redirect non-logged in users to external SSO login page, then I would do it differently. I would not do redirecting in logout button, but instead implement it in access control of the views using BeforeEnterEvent, you need to implement BeforeEnterObserver interface in the view and override beforeEnter(..) method as follows:

@Override
public void beforeEnter(BeforeEnterEvent event) {
    if (VaadinSession.getCurrent().getAttribute("userLoggedIn") == null) {
        UI.getCurrent().getPage().executeJavaScript("window.open(\"http://vaadin.com/\", \"_self\");");
    }
}
Tatu Lund
  • 9,949
  • 1
  • 12
  • 26
  • 1
    I am curious, what would be the best way to make such an anchor look like the usual button? – Steffen Harbich Sep 13 '18 at 07:05
  • how do I execute my custom code on the click? And how do I make it look like a button ? – Tyvain Sep 13 '18 at 19:56
  • I managed to make the custom code working: "Anchor anch = new Anchor("https://xxxx/cas/logout", "Déconnexion"); anch.getElement().addEventListener("click", e -> { System.out.println("CLICK !! " + e) ; });" – Tyvain Sep 13 '18 at 21:34
  • Still not what I asked, I need a Button look (with icon etc..) and doing it in java. I tried another approch: not display the anchor, and fireEvent from the Button, but it doesn't work... An example would be great here. – Tyvain Sep 13 '18 at 23:53
  • @Tyvain, thanks for additional info, I understand your use case better and added more advice accordingly. – Tatu Lund Sep 14 '18 at 08:51