1

In my project I have a jpa database connection where I store exercises. I read all exercises from the database and store them into the variable allExercises. Afterwards I save the title of each exercise in a button, add them to a box for the GUI and also add a click listener event that handles the navigation to the next view. This next view is a detail view of the chosen exercise. To know on which exercise I have clicked I want to pass the id of the exercise object as a parameter. For now I have following code:

for(Exercise exercise : allExercises) { // create new button with exercise title Button bt = new Button(exercise.getTitle()); // add button to box boxExerciseTitle.add(bt); // add action event bt.addClickListener(e -> UI.getCurrent().navigate(ExerciseDetailView.class, exercise.getId())); } 

This code is not working. There is a problem with the ID respectively that the navigate function does not handle this two arguments.

My goal is to read the parameter in the detail view like that:

public class ExerciseDetailView extends VerticalLayout implements View{ @Override public void enter(ViewChangeEvent event) { String myPassedId = event.getParameters(); ... } } 

How can I change my code that I can pass the id as a parameter in Vaadin?

2
  • Which vaadin version are you using? Commented Jan 6, 2020 at 11:40
  • @KasparScherrer: I am using vaadin version 14 Commented Jan 6, 2020 at 11:55

1 Answer 1

1

The detail view must implement HasUrlParameter and then you get the parameter in the setParameter method:

@Route(value = "exercise") public class ExerciseDetailView extends VerticalLayout implements HasUrlParameter<Long> { @Override public void setParameter(BeforeEvent event, Long exerciseId) { // find the single exercise using the given id // Exercise exercise = exerciseRepository.findById(exerciseId); } } 

Documentation: https://vaadin.com/docs/v14/flow/routing/tutorial-router-url-parameters.html

If the type of exercise.getId() is not Long but instead Integer or another Number, then change the type of HasUrlParameter accordingly.

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

6 Comments

@KasparScherrer Probably but from the question I can't see the type of id. I used String because of this line: String myPassedId = event.getParameters();
@KasparScherrer Ah ok. I don't know Vaadin < 10 :-)
If you used HasUrlParameter<String>, then you would have to change .navigate(ExerciseDetailView.class, exercise.getId()) to .navigate(ExerciseDetailView.class, exercise.getId().toString()) too.
You still assume that id is Long. It could be of any datatype :-)
I took the liberty of adding this into the answer. If you disagree, feel free to revert it to your version. I'm deleting all comments here as well :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.