Vaadin中带参数的导航

huangapple go评论73阅读模式
英文:

Navigation in vaadin with parameters

问题

在我的项目中,我有一个JPA数据库连接,用于存储练习。我从数据库中读取所有的练习,并将它们存储在变量allExercises中。然后,我将每个练习的标题保存在一个按钮中,将它们添加到GUI的一个框中,并添加一个点击监听器事件来处理导航到下一个视图。这个下一个视图是所选练习的详细视图。为了知道我点击了哪个练习,我想将练习对象的id作为参数传递。目前我有以下代码:

for(Exercise exercise : allExercises) {
    // 创建带有练习标题的新按钮
    Button bt = new Button(exercise.getTitle());
    // 将按钮添加到框中
    boxExerciseTitle.add(bt);
    // 添加动作事件
    bt.addClickListener(e -> UI.getCurrent().navigate(ExerciseDetailView.class, exercise.getId()));
}

这段代码不起作用。问题在于ID,或者说导航函数不能处理这两个参数。

我的目标是在详细视图中以以下方式读取参数:

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

如何更改我的代码,以便我可以在Vaadin中将ID作为参数传递?

英文:

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?

答案1

得分: 1

详细视图必须实现 HasUrlParameter,然后在 setParameter 方法中获取参数:

@Route(value = "exercise")
public class ExerciseDetailView extends VerticalLayout
        implements HasUrlParameter<Long> {

    @Override
    public void setParameter(BeforeEvent event,
                             Long exerciseId) {
        // 使用给定的ID查找单个练习
        // Exercise exercise = exerciseRepository.findById(exerciseId);
    }
}

文档链接:https://vaadin.com/docs/v14/flow/routing/tutorial-router-url-parameters.html

如果 exercise.getId() 的类型不是 Long,而是 Integer 或其他数字类型,请相应地更改 HasUrlParameter 的类型。

英文:

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

@Route(value = &quot;exercise&quot;)
public class ExerciseDetailView  extends VerticalLayout
        implements HasUrlParameter&lt;Long&gt; {

    @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.

huangapple
  • 本文由 发表于 2020年1月6日 18:44:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定