【问题标题】:Navigation in vaadin with parameters带参数的 vaadin 导航
【发布时间】:2020-01-06 10:44:25
【问题描述】:

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

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()));
}

此代码不起作用。 ID分别存在问题,导航函数不处理这两个参数。

我的目标是像这样读取详细视图中的参数:

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

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

【问题讨论】:

  • 您使用的是哪个 vaadin 版本?
  • @KasparScherrer:我使用的是 vaadin 版本 14

标签: navigation parameter-passing vaadin vaadin-flow


【解决方案1】:

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

@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);
    }
}

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

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

【讨论】:

  • @KasparScherrer 可能但从问题中我看不到 id 的类型。我使用 String 因为这行: String myPassedId = event.getParameters();
  • @KasparScherrer 好的。我不知道 Vaadin
  • 如果您使用HasUrlParameter&lt;String&gt;,那么您也必须将.navigate(ExerciseDetailView.class, exercise.getId()) 更改为.navigate(ExerciseDetailView.class, exercise.getId().toString())
  • 你仍然假设 id 是 Long。它可以是任何数据类型 :-)
  • 我冒昧地将其添加到答案中。如果您不同意,请随时将其恢复为您的版本。我也在这里删除所有 cmets :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
相关资源
最近更新 更多