在示例vaadin中收到错误

时间:2010-10-20 16:34:21

标签: java vaadin

我是vaadin的新手,我编译了2个代码,其中一个是

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进行开发...任何人都可以告诉我,我发现了这个错误

1 个答案:

答案 0 :(得分:1)

您的MenuApplication应该扩展com.vaadin.Application而不是VerticalLayout,以便能够将其用作Application。

然而,基于上面的示例,我将改变WEB-INF / web.xml以改为使用HelloWorld类,并将init()函数更改为包括如下内容:

main.addComponent(new MenuApplication());

(也考虑重命名MenuApplication,因为它不是应用程序,而是VerticalLayout)