2010-10-20 167 views
0

我是新來vaadin我編2碼之一是越來越例子vaadin錯誤

import com.vaadin.ui.Label; 
import com.vaadin.ui.Window; 

@SuppressWarnings("serial") 
public class HelloWorld extends com.vaadin.Application { 

    /** 
    * Init is invoked on application load (when a user accesses the application 
    * for the first time). 
    */ 
    @Override 
    public void init() { 

     // Main window is the primary browser window 
     final Window main = new Window("Hello window"); 
     setMainWindow(main); 

     // "Hello world" text is added to window as a Label component 
     main.addComponent(new Label("Hello World!")); 
    } 
} 

它編譯和工作正常,另一個是

import com.vaadin.terminal.ExternalResource; 
import com.vaadin.ui.MenuBar; 
import com.vaadin.ui.MenuBar.Command; 
import com.vaadin.ui.MenuBar.MenuItem; 
import com.vaadin.ui.VerticalLayout; 

@SuppressWarnings("serial") 
public class MenuApplication extends VerticalLayout { 

    private MenuBar menubar = new MenuBar(); 

    public MenuApplication() { 

     // Save reference to individual items so we can add sub-menu items to 
     // them 
     final MenuBar.MenuItem file = menubar.addItem("File", null); 
     final MenuBar.MenuItem newItem = file.addItem("New", null); 
     file.addItem("Open file...", menuCommand); 
     file.addSeparator(); 

     newItem.addItem("File", menuCommand); 
     newItem.addItem("Folder", menuCommand); 
     newItem.addItem("Project...", menuCommand); 

     file.addItem("Close", menuCommand); 
     file.addItem("Close All", menuCommand); 
     file.addSeparator(); 

     file.addItem("Save", menuCommand); 
     file.addItem("Save As...", menuCommand); 
     file.addItem("Save All", menuCommand); 

     final MenuBar.MenuItem edit = menubar.addItem("Edit", null); 
     edit.addItem("Undo", menuCommand); 
     edit.addItem("Redo", menuCommand).setEnabled(false); 
     edit.addSeparator(); 

     edit.addItem("Cut", menuCommand); 
     edit.addItem("Copy", menuCommand); 
     edit.addItem("Paste", menuCommand); 
     edit.addSeparator(); 

     final MenuBar.MenuItem find = edit.addItem("Find/Replace", menuCommand); 

     // Actions can be added inline as well, of course 
     find.addItem("Google Search", new Command() { 
      public void menuSelected(MenuItem selectedItem) { 
       getWindow().open(new ExternalResource("http://www.google.com")); 
      } 
     }); 
     find.addSeparator(); 
     find.addItem("Find/Replace...", menuCommand); 
     find.addItem("Find Next", menuCommand); 
     find.addItem("Find Previous", menuCommand); 

     final MenuBar.MenuItem view = menubar.addItem("View", null); 
     view.addItem("Show/Hide Status Bar", menuCommand); 
     view.addItem("Customize Toolbar...", menuCommand); 
     view.addSeparator(); 

     view.addItem("Actual Size", menuCommand); 
     view.addItem("Zoom In", menuCommand); 
     view.addItem("Zoom Out", menuCommand); 

     addComponent(menubar); 
    } 

    private Command menuCommand = new Command() { 
     public void menuSelected(MenuItem selectedItem) { 
      getWindow().showNotification("Action " + selectedItem.getText()); 
     } 
    }; 

它獲得誤差

javax.servlet.ServletException: java.lang.ClassCastException: com.example.menu.MenuApplication cannot be cast to com.vaadin.Application 
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.handleServiceException(AbstractApplicationServlet.java:966) 
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:523) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

root cause 

java.lang.ClassCastException: com.example.menu.MenuApplication cannot be cast to com.vaadin.Application 
com.vaadin.terminal.gwt.server.ApplicationServlet.getNewApplication(ApplicationServlet.java:82) 
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.createApplication(AbstractApplicationServlet.java:940) 
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.findApplicationInstance(AbstractApplicationServlet.java:768) 
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:431) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

我正在使用tomcat和eclipse進行我的開發......任何人都可以告訴我yi cam得到這個錯誤

+0

'com.example.menu.MenuApplication不能轉換爲com.vaadin.Application' - 看起來您正在嘗試執行非法類型轉換。 – 2010-10-20 16:37:07

回答

1

您的MenuApplication應該擴展com.vaadin.Application而不是VerticalLayout以便能夠將其用作Application。

但是基於上面的示例我會改變的WEB-INF/web.xml中使用的HelloWorld類,而不是和改變init()函數,包括像:

main.addComponent(new MenuApplication()); 

(也考慮重新命名MenuApplication,因爲它不是一個應用程序,而是一個VerticalLayout)

+0

這聽起來像是一個好方法。帶擴展應用程序的類是應該包含在web.xml文件路徑中的類。其他文件,例如你的MenuSection/-Layout /就像任何其他Vaadin組件一樣,它應該被添加到你的Vaadin應用程序的窗口或其他組件容器中。 – 2010-10-21 12:24:54

+0

每個vaadin應用程序都必須擴展Application類 – 2011-01-20 12:56:19