Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the main view of a Vaadin 7 application?

Tags:

vaadin

vaadin7

I want to write a Vaadin 7 application (see MyVaadinUI below), which asks the user to enter user name and password.

If they are correct, another view (see MainUI below) should appear and take the entire area (replace the login view).

I tried to implement this transition in the method MyVaadinUI.goToMainWindow, but I get the error

java.lang.RuntimeException: Component must be attached to a session when getConnectorId() is called for the first time
    at com.vaadin.server.AbstractClientConnector.getConnectorId(AbstractClientConnector.java:417)
    at com.vaadin.server.communication.ConnectorHierarchyWriter.write(ConnectorHierarchyWriter.java:67)
    at com.vaadin.server.communication.UidlWriter.write(UidlWriter.java:143)
    at com.vaadin.server.communication.UidlRequestHandler.writeUidl(UidlRequestHandler.java:149)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:97)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)

when I run the application and press the button.

How can I fix it?

@Theme("mytheme")
@SuppressWarnings("serial")
public class MyVaadinUI extends UI
{
    private TextField userNameTextField;
    private PasswordField passwordTextField;

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "ru.mycompany.vaadin.demo.AppWidgetSet")
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        addUserNameTextField(layout);
        addPasswordTextField(layout);

        addButton(layout, request);
    }

    private void addPasswordTextField(Layout aLayout) {
        passwordTextField = new PasswordField("Пароль:");

        aLayout.addComponent(passwordTextField);

    }

    private void addUserNameTextField(final Layout aLayout) {
        userNameTextField = new TextField("Пользователь:");

        aLayout.addComponent(userNameTextField);
    }


    private void addButton(final Layout aParent, final VaadinRequest request) {
        final Button button = new Button("Войти");
        button.addClickListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                final boolean credentialsCorrect = checkCredentials();

                if (credentialsCorrect) {
                    goToMainWindow(request);
                } else {
                    [...]
                }
            }
        });
        aParent.addComponent(button);
    }

    private void goToMainWindow(final VaadinRequest aRequest) {

        final MainUI mainUI = new MainUI();
        mainUI.init(aRequest);
        setContent(mainUI);
    }

}

@Theme("mytheme")
@SuppressWarnings("serial")
public class MainUI extends UI {
    @Override
    protected void init(final VaadinRequest vaadinRequest) {
        final HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();

        setContent(splitPanel);

        splitPanel.setSizeFull();
        splitPanel.setSplitPosition(200, Unit.PIXELS);

        final String[] tabLabels = new String[] {
                "Tree item 1",
                "Tree item 2"};

        final Tree tree = new Tree();

        for (int i=0; i < tabLabels.length; i++)
        {
            addTreeItem(tree, tabLabels[i]);
        }

        splitPanel.setFirstComponent(tree);
        splitPanel.setSecondComponent(new Label("Test"));
    }

    private void addTreeItem(final Tree aTree, final String aLabel) {
        aTree.addItem(aLabel);
    }
}
like image 890
Dmitrii Pisarenko Avatar asked Dec 26 '22 19:12

Dmitrii Pisarenko


2 Answers

On the Vaadin forum someone suggested to use the navigator, which solved my problem.

like image 139
Dmitrii Pisarenko Avatar answered Dec 31 '22 13:12

Dmitrii Pisarenko


I'd rather think that MainUI should extend HorizontalSplitPanel, not UI. It is strange concept to me to insert one UI into another.

like image 34
michaldo Avatar answered Dec 31 '22 13:12

michaldo