简体   繁体   English

Vaadin 视图 - 无法解析导航器

[英]Vaadin views - navigator can't be resolved

I'm new to Vaadin.我是 Vaadin 的新手。 I'm currently writing a simple server side app.我目前正在编写一个简单的服务器端应用程序。 I'm learning from Vaadin site docs and a current example of navigation doesn't work.我正在从 Vaadin 站点文档中学习,但当前的导航示例不起作用。 I'm not able to compile and run/debug the app.我无法编译和运行/调试应用程序。

This is my main UI class:这是我的主要 UI 类:

@Theme("mytheme")
public class MyUI extends UI {

    Navigator navigator;
    protected static final String MAINVIEW = "main";

    @Override
    protected void init(VaadinRequest request) {
        getPage().setTitle("Navigation Example");

        // Create a navigator to control the views
        navigator = new Navigator(this, this);

        // Create and register the views
        navigator.addView("", new StartView());
        navigator.addView(MAINVIEW, new MainView());
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

I dont have any problems here.我在这里没有任何问题。 I have implemented StartView class and that is where I get an error.我已经实现了 StartView 类,这就是我收到错误的地方。 At navigator.navigateTo(MAINVIEW);navigator.navigateTo(MAINVIEW); There are 2 errors有2个错误

navigator can't be resolved导航无法解决

and

MAINVIEW cannot be resolved to variable MAINVIEW 无法解析为变量

I read everything many times and tried to found the solution, but nothing really seems to work.我多次阅读所有内容并试图找到解决方案,但似乎没有任何效果。

public class StartView extends VerticalLayout implements View {
    public StartView() {
        setSizeFull();

        Button button = new Button("Go to Main View",
                new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                navigator.navigateTo(MAINVIEW);
            }
        });
        addComponent(button);
        setComponentAlignment(button, Alignment.MIDDLE_CENTER);
    }

    @Override
    public void enter(ViewChangeEvent event) {
        Notification.show("Welcome to the Animal Farm");
    }
}

Exactly the same thing happens in MainView class.完全相同的事情发生在 MainView 类中。

@DesignRoot
public class MainView extends VerticalLayout implements View {
    // Menu navigation button listener
    class ButtonListener implements Button.ClickListener {
        String menuitem;
        public ButtonListener(String menuitem) {
            this.menuitem = menuitem;
        }

        @Override
        public void buttonClick(ClickEvent event) {
            // Navigate to a specific state
            navigator.navigateTo(MAINVIEW + "/" + menuitem);
        }
    }

    VerticalLayout menuContent;
    Panel equalPanel;
    Button logout;

    public MainView() {
        Design.read(this);

        menuContent.addComponent(new Button("Pig",
                  new ButtonListener("pig")));
        menuContent.addComponent(new Button("Cat",
                  new ButtonListener("cat")));
        menuContent.addComponent(new Button("Dog",
                  new ButtonListener("dog")));
        menuContent.addComponent(new Button("Reindeer",
                  new ButtonListener("reindeer")));
        menuContent.addComponent(new Button("Penguin",
                  new ButtonListener("penguin")));
        menuContent.addComponent(new Button("Sheep",
                  new ButtonListener("sheep")));

        // Allow going back to the start
        logout.addClickListener(event -> // Java 8
            navigator.navigateTo(""));
    }

    @DesignRoot
    class AnimalViewer extends VerticalLayout {
        Label watching;
        Embedded pic;
        Label back;

        public AnimalViewer(String animal) {
            Design.read(this);

            watching.setValue("You are currently watching a " +
                              animal);
            pic.setSource(new ThemeResource(
                "img/" + animal + "-128px.png"));
            back.setValue("and " + animal +
                " is watching you back");
        }
    }

    @Override
    public void enter(ViewChangeEvent event) {
        if (event.getParameters() == null
            || event.getParameters().isEmpty()) {
            equalPanel.setContent(
                new Label("Nothing to see here, " +
                          "just pass along."));
            return;
        } else
            equalPanel.setContent(new AnimalViewer(
                event.getParameters()));
    }
}

Could you tell me what am I doing wrong?你能告诉我我做错了什么吗? I can't understand why it can't go through.我不明白为什么它不能通过。

Problems you are facing are actually basics of Java.您面临的问题实际上是 Java 的基础知识。

First error:第一个错误:

navigator can't be resolved导航无法解决

StartView and MainView don't get navigator instance. StartViewMainView没有获得navigator实例。 You can pass it by setter or constructor or you can get to navigator this way:您可以通过 setter 或构造函数传递它,也可以通过以下方式进入导航器:

UI.getCurrent().getNavigator()

Secondly, there is actually no need to create instance of Navigator , because you can call line above in your MyUI , so it will look like this:其次,实际上不需要创建Navigator实例,因为您可以在MyUI调用 line MyUI ,所以它看起来像这样:

    protected void init(VaadinRequest request) {
        getPage().setTitle("Navigation Example");

        // Create and register the views
        UI.getCurrent().getNavigator().addView("", new StartView());
        UI.getCurrent().getNavigator().addView(MAINVIEW, new MainView());
    }

@Edit @编辑

If its about next error:如果是关于下一个错误:

MAINVIEW cannot be resolved to variable MAINVIEW 无法解析为变量

If you are using static fields, you should access to them this way:如果您使用的是静态字段,您应该通过以下方式访问它们:

ClassName.FIELD

So in your example (as long as you will pass navigator to your Main and Start View classes:所以在你的例子中(只要你将navigator传递给你的MainStart View 类:

navigator.navigateTo(MyUI.MAINVIEW);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM