我在Vaadin和JavaEE中都很新,我认为我有基本的问题,但很乐意帮忙。
我的Vaadin项目有3个课程,他们在这里: 主类,仅响应启动和创建导航器:
public class MyprojectUI extends UI {
public Navigator navigator;
public static final String SECOND_VIEW = "SecondView";
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MyprojectUI.class)
public static class Servlet extends VaadinServlet {
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
ComponentContainerViewDisplay viewDisplay = new ComponentContainerViewDisplay(layout);
navigator = new Navigator(UI.getCurrent(), viewDisplay);
navigator.addView("", new FirstView());
navigator.addView(SECOND_VIEW, new SecondView());
}
}
两个观点类:
public class FirstView extends HorizontalLayout implements View {
TextArea text = new TextArea();
Button button = new Button("go");
@Override
public void enter(ViewChangeEvent event) {
this.addComponent(text);
this.addComponent(button);
button.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
getUI().getNavigator().navigateTo(MyprojectUI.SECOND_VIEW);
}
});
}
}
-
public class SecondView extends HorizontalLayout implements View {
Label label = new Label("Passed text here");
@Override
public void enter(ViewChangeEvent event) {
this.addComponent(label);
}
}
如何将TextArea中的数据传递到第二个视图?我希望在单击“go”按钮时将标签文本设置为TextArea中的文本。
感谢您的帮助。
答案 0 :(得分:5)
我建议您应用Model-View-Presenter模式。查看已经完成。这里不需要模型。只需写一个演示者。 在MVP中,Presenter由视图触发,然后演示者从视图中收集所需的数据,进行一些计算和更新视图。在目前的情况下,它可能看起来像:
class Presenter {
FirstView firstView;
SecondView secondView;
public Presenter (FirstView firstView, SecondView secondView) {
this.firstView = firstView;
this.secondView = secondView;
}
public void goClicked(UI ui) {
secondView.label.setValue(firstView.text.getValue());
ui.getNavigator().navigateTo(MyprojectUI.SECOND_VIEW);
}
}
代码被简化,推荐的做法是定义视图的接口而不是直接字段访问,但主要思想是相同的。
答案 1 :(得分:1)
SecondView secondView = new SecondView();
navigator.addView(SECOND_VIEW, secondView );
Presenter presenter = new Presenter(firstView, secondView);