2017-09-24 172 views
0

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

你能告訴我我做錯了什麼嗎?我不明白爲什麼它不能通過。

+0

你確定MAINVIEW在範圍內嗎? – jdv

回答

0

您正面臨的問題實際上是Java的基礎知識。

第一個錯誤:

navigator can't be resolved

StartViewMainView沒有得到navigator實例。您可以通過制定者或構造函數傳遞,或者你可以去導航儀這樣說:

UI.getCurrent().getNavigator() 

其次,其實是沒有必要創建的Navigator實例,因爲你可以在你的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

如果其對下一個錯誤:

MAINVIEW cannot be resolved to variable

只要你會通過 navigatorMainStart視圖類

ClassName.FIELD 

所以,在你的榜樣(:

如果使用的是靜態字段,你應該訪問它們這樣

navigator.navigateTo(MyUI.MAINVIEW); 
相關問題