Vaadin 14 re-routing

Viewed 525

I'm working on a Vaadin 14 application. I want users to be able to go to a certain URL and, depending on certain conditions, get rerouted to one of several views which have their own route.

I know how re-routing works using BeforeEnterHandler / BeforeEnterObserver. But this works only to interrupt navigation to a known route, which as far as I can see needs to be linked to a class implementing Component either by using @Route as an annotation for that class or dynamically registered. Does this mean I need to create a dummy Component implementation with this route, which will never be shown, just to be able to reroute in a BeforeEnterHandler? Or is there some cleaner way to hook into the routing lifecycle and reroute even before the corresponding view component is looked up?

Update: I wrote this question with re-routing in mind, but I found out I will use forwarding instead since forwardTo updates the browser URL as well, and that is what I want. But the question remains the same otherwise, since I still need a route to be able to forward to another one.

Update 2: Since I need to supply query parameters to the target views, I can't even use forwardTo. UI.navigate apparently doesn't work correctly when called during navigation, so I needed to handle an AfterNavigationEvent and call UI.navigate there. This means navigation is already done at this point so my route definitely needs a Component as a navigation target. Both these issues (no route handling possible without creating a dummy UI component and forwardTo not accepting query parameters) seem like basic shortcomings of the Vaadin framework's routing support.

1 Answers

In my opinion, using a route is the cleanest way to do it and it's aligned with the concepts in the framework—anything (in Vaadin) that "listens" to a URL is a route. You can define routes with @Route or you can register them dynamically with RouteConfiguration if that helps. Another option is to use a Servlet to redirect to the desired view, but I don't think that's cleaner.

Related