Call view with query parameters

Hello,

I want to call a view with some query parameters. So I looked in the documentation (V16) and found the RouterLink class.
But the RouterLink class allows only to provide a path parameter? How do I call a view with query parameters?
Do I really need to do this?

QueryParameters params = QueryParameters.simple(Map.of("foo", "bar"));
String url = UI.getCurrent().getRouter().get().getUrl(MyComponent.class);
Anchor link = new Anchor(url + "?" + params.getQueryString(), "link");

Dont get me wrong. I like the concept of Vaadin, BUT I find the parameter handling extremely complex for a Web Framework.
It’s like the focus is totally something else and not the web. There are no Utility classes to help compose parameters. You do not know
how to use the classes because the documentation is lacking examples on this.

And why not using already established concepts from other frameworks???

Thnaks

If you’re in a web app…we navigate using the navigate and the ComponentUtil to pass objects around:

                        ComponentUtil.setData(UI.getCurrent(), Club.class, club);
                        getUI().get().navigate(ClubDues.class);
						...then in our ClubDues it does:
						        Club club = ComponentUtil.getData(UI.getCurrent(), Club.class);

Thanks for this approach! I still like seeing url parameters

I have to say, to use ComponentUtil.set/getData looks more like a hack to me.

Depends on your viewpoint. I would argue that adding parameters and their values to an URL is a hack, and it can even be exploited by users who recognize the pattern.

I didn’t try it but it looks like you can add QueryParameters in RouterLink:

RouterLink l = new RouterLink("link", MainView.class);
        QueryParameters params = QueryParameters.simple(Map.of("foo", "bar"));
        l.setQueryParameters(params);

For internal link, you should use Routerlink since it does not refresh the entire page.

But as you said the documentation is lacking of examples of QueryParameters: I found only a usage of query parameters but no usage of RouterLink with QueryParameters.

Does this look better? Kaspar’s example is great for Object parameters and if you want to hide parameters from the user.

Jean-Christophe Gueriaud:
I didn’t try it but it looks like you can add QueryParameters in RouterLink:

RouterLink l = new RouterLink("link", MainView.class);
        QueryParameters params = QueryParameters.simple(Map.of("foo", "bar"));
        l.setQueryParameters(params);

For internal link, you should use Routerlink since it does not refresh the entire page.

But as you said the documentation is lacking of examples of QueryParameters: I found only a usage of query parameters but no usage of RouterLink with QueryParameters.

Does this look better? Kaspar’s example is great for Object parameters and if you want to hide parameters from the user.

Thank you! I totally missed the setQueryParameters() method.
I tested it and this is what I was looking for.

Kaspar Scherrer:
Depends on your viewpoint. I would argue that adding parameters and their values to an URL is a hack, and it can even be exploited by users who recognize the pattern.

The users should be able to manipulate some parameters.

Bob Yacobucci:
If you’re in a web app…we navigate using the navigate and the ComponentUtil to pass objects around:

                        ComponentUtil.setData(UI.getCurrent(), Club.class, club);
                        getUI().get().navigate(ClubDues.class);
  					...then in our ClubDues it does:
  					        Club club = ComponentUtil.getData(UI.getCurrent(), Club.class);

This helped me! :slight_smile: