Search code examples
javaspring-bootvaadinvaadin8

Vaadin pass parameter to view


I am currently making a form for film database, using Vaadin. My problem is the whole situation with deep linking. I want to have a url with an id of movie in it, to make possible to get to the form of concrete movie.

localhost:port/filmView/idOfSelectedMovie

I am currently using pushState but there are couple of problems.

1) When I add string "Film/" before Id, the first selection works fine, however with following selections the url just keeps adding this.

http://localhost:8081/Film/Film/Film/Film/Film/4

2) The second option I tried was using just an id. The effect was that the url just keeps the host with port, and gets rid of ViewName

http://localhost:8801/4

I had already tried to use replaceState and Urifragment methods, the effect wasn't better at all.

the function handling selection of movie on the list

 this.itemsList.addSelectionListener(selectionEvent -> {
            if (selectionEvent.getFirstSelectedItem().isPresent()) {
                Film selectedFilm = selectionEvent.getFirstSelectedItem().get();
                this.setupForm(selectedFilm);
                Page.getCurrent().pushState("Film/" + selectedFilm.getFilmId());

            }
        });

Solution

  • What you have to do while navigating between views this: let's say you have a View with a list of films, from you can select a film. When you select a film from that list, you move to the film View

    getUI().getNavigator().navigateTo(FilmView + "/" + filmId); //let's say id 88
    

    in this way, you will navigate to http://localhost:8081/Film/88

    now, in your FilmView you can get and use this id, something like:

    public class FilmView extends VerticalLayout implements View{
        @Override
        public void enter(ViewChangeEvent event) {
            String yourPassedId = event.getParameters();
            //do stuff with your id, for example loading from DB
        }
    }
    

    In this way, you can reach every film you want with hard link, like http://localhost:8081/Film/88