Create a non-visual page which redirects automatically to the root page aft

Hi all,

I am fairly new to Vaadin, so please bear with me.

I want to create a non-visual URL endpoint at location /callback?code=... which:

  1. reads out the query parameter code
  2. sets the value of query parameter code in the session context
  3. redirect to /

I already found out that:

But I am having troubles to create a route which combines everything.

Is there an example or basic example out there somewhere? FWIW, I am starting from the sample project as given on https://vaadin.com/start/latest/simple-ui.

I currently have the following code:

@Route(value = "code")
@PageTitle("Code")
public class CodePage extends VerticalLayout implements BeforeEnterObserver {
    public CodePage() {
    }

    @Override
    public void beforeEnter(final BeforeEnterEvent event) {
        String code = event.getLocation().getQueryParameters().getParameters().get("code").get(0);

		event.getUI().getSession().setAttribute("code", code);
        event.rerouteTo(ReviewsList.class);
    }
}

Is this a good start?

I am also wondering if there is a way to not extend from VerticalLayout or any kind of layout, as this page will only be redirecting and thus this page will not display anything.

I am using Vaadin version 12.0.3.

Thank you, greatly appreciated!

There are basically two alternatives you can use easily. The other is like above, i.e. use rerouteTo(…) in beforeEnter(…) method. If you do not need to process query parameters or do anything, then you can also use @RouteAlias annotation.

Tatu Lund:
There are basically two alternatives you can use easily. The other is like above, i.e. use rerouteTo(…) in beforeEnter(…) method. If you do not need to process query parameters or do anything, then you can also use @RouteAlias annotation.

Thank you, appreciated!

Is there a way to route but also change the URL in the browser (in this case to /)? Now when I do event.rerouteTo(ReviewsList.class), the URL is unchanged and still set to the original location.

Is there a way to route but also change the URL in the browser

There should not be no need, since that was a bug, that has been recently fixed, see the issue here: https://github.com/vaadin/flow/issues/4189

Thank you, that’s good to know!

Is it common practice to implement VerticalLayout, even though I am just redirecting and never showing anything?

Here in my case, the URL endpoint /code is only used to receive a code from an external source, remember it in the session and then route to the main/root of the application. So I was wondering if an alternative (as replacement for implementing VerticalLayout) could be more optimal.

Hi Jos!

I’m having the same problem when i redirect to another view, the URL change but the page is totally white.

I’m redirecting with UI.getCurrent().navigate(CompanyView.class);

Thanks!