我是Vaadin的新手。我目前正在寫一個簡單的服務器端應用程序。我從Vaadin網站文檔中學習,當前的導航示例不起作用。我無法編譯和運行/調試應用程序。Vaadin views - navigator can not be resolved
這是我的主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 {
}
}
我沒有在這裏有任何問題。我已經實現了StartView類,這是我得到一個錯誤。在navigator.navigateTo(MAINVIEW);
有2個錯誤
navigator can't be resolved
和
MAINVIEW cannot be resolved to variable
我讀到的一切多次,並試圖找到解決方案,但沒有什麼似乎工作。
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");
}
}
完全相同的事情發生在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()));
}
}
你能告訴我我做錯了什麼嗎?我不明白爲什麼它不能通過。
你確定MAINVIEW在範圍內嗎? – jdv