2

I'm new to Vaadin and trying to understand how to make View to get several parameters from URL. For example

http://www.some.com/book/18/page/41 

Numbers 18 and 41 are parameters.

I've found that I can implement HasUrlParameter<T> and then use setParameter method, but it can be used only for one parameter.

4 Answers 4

2

It seems like Vaadin 14 has got an update and got support for multiple path parameters.

Example:

@Route("user/:userID/:messageID/edit") public class UserProfileEdit extends Div implements BeforeEnterObserver { private String userID; private String messageID; @Override public void beforeEnter(BeforeEnterEvent event) { userID = event.getRouteParameters().get("userID").get(); messageID = event.getRouteParameters().get("messageID").get(); } } 

Source: https://vaadin.com/docs/v14/flow/routing/tutorial-router-templates

Sign up to request clarification or add additional context in comments.

Comments

1

Are you using @WildcardParameter in your setParameter method? Wildcard URL parameters

Assuming that greet (The book in your case) is the route, then the code below sets 18\page\41. Since it's a string you would need to parse it and extract values you need, but the value is there.

@Route("greet") public class WildcardGreeting extends Div implements HasUrlParameter<String> { @Override public void setParameter(BeforeEvent event, @WildcardParameter String parameter) { if (parameter.isEmpty()) { setText("Welcome anonymous."); } else { setText(String.format( "Handling parameter %s.", parameter)); } } } 

P.S. Not related to the question, but looking at your URL, could it be that query parameters suit you better Query parameters?

1 Comment

Query parameters are a possible way, but I have 2 more nesting entities, so it would be better to have them just as URL parameters, I think.
1

There is no built-in suppor for having multiple parameters for Java views in Vaadin. What you can do is to annotate the parameter with @WildcardParameter so that multiple path segments can be captured into one parameter. You would then have to manually manage the contents of that value - concatenating strings when generating URLs and parsing strings in setParameter.

Support for multiple parameters is being worked on right now, but the work is not yet completed. It is not yet clear which future version of Vaadin will get this feature, but my guess right now is that it would be either version 14.3 or 14.4.

Comments

0

A simple example with the solution

@Route("book") public class BookView extends Div implements HasUrlParameter<String> { @Override public void setParameter(BeforeEvent event, @WildcardParameter String parameter) { if (!parameter.isEmpty()) { String params[] = parameter.split("/"); if (params.length == 1) { // Do something .. } else if (params.length == 2) { // Do another thing .. } else { // Do something else } } } } 

The link can be created like this:

new RouterLink("No params", BookView.class); new RouterLink("One param", BookView.class, "18"); new RouterLink("Two param", BookView.class, "18/edit"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.