Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 导航问题_Java_Vaadin_Vaadin7 - Fatal编程技术网

Java 导航问题

Java 导航问题,java,vaadin,vaadin7,Java,Vaadin,Vaadin7,我正在使用链接为原型构建我的新Vaadin应用程序 我有三种观点 1) 登录视图 2) 主视图 3) 注册视图 及 1) 处理导航的基类LoginUI.java 所以,总而言之,我有一个登录视图,它是主登录屏幕,有两个按钮,一个用于登录应用程序,另一个用于注册新用户 这3个视图由一个名为LoginUI.java的基类处理 我目前面临的问题是,我能够成功登录到应用程序,即从登录视图到主视图的导航非常好。但是,当我单击“注册”按钮时,导航根本不会将我带到“注册”视图。它仍然将我保持在登录视图(而不是

我正在使用链接为原型构建我的新Vaadin应用程序

我有三种观点

1) 登录视图 2) 主视图 3) 注册视图

1) 处理导航的基类LoginUI.java

所以,总而言之,我有一个登录视图,它是主登录屏幕,有两个按钮,一个用于登录应用程序,另一个用于注册新用户

这3个视图由一个名为LoginUI.java的基类处理

我目前面临的问题是,我能够成功登录到应用程序,即从登录视图到主视图的导航非常好。但是,当我单击“注册”按钮时,导航根本不会将我带到“注册”视图。它仍然将我保持在登录视图(而不是导航到注册视图)

谁能帮我一下吗

我认为问题主要在于LoginUI.java中的beforeViewChange方法

但我无法修复它

课程如下

1)LoginUI.java

public class LoginUI extends UI {

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = LoginUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {

    //
    // Create a new instance of the navigator. The navigator will attach
    // itself automatically to this view.
    //
    new Navigator(this, this);

    //
    // The initial log view where the user can login to the application
    //
    getNavigator().addView(SimpleLoginView.NAME, SimpleLoginView.class);

    getNavigator().addView(RegistrationView.NAME, RegistrationView.class);
    //
    // Add the main view of the application
    //
    getNavigator().addView(MainView.NAME, MainView.class);

    //
    // We use a view change handler to ensure the user is always redirected
    // to the login view if the user is not logged in.
    //
    getNavigator().addViewChangeListener(new ViewChangeListener() {

        @Override
        public boolean beforeViewChange(ViewChangeEvent event) {

            // Check if a user has logged in
            System.out.println("getSession().getAttribute(\"user\") is " + getSession().getAttribute("user"));
            boolean isLoggedIn = getSession().getAttribute("user") != null;
            boolean isLoginView = event.getNewView() instanceof SimpleLoginView;

            System.out.println("isLoggedIn is " + isLoggedIn);
            System.out.println("isLoginView is " + isLoginView);

            if (!isLoggedIn && !isLoginView) {
                // Redirect to login view always if a user has not yet
                // logged in
                //getNavigator().navigateTo(SimpleLoginView.NAME);
                getNavigator().navigateTo(SimpleLoginView.NAME);
                return false;

            } else if (isLoggedIn && isLoginView) {
                // If someone tries to access to login view while logged in,
                // then cancel
                return false;
            }

            System.out.println("Returning True");
            return true;
        }

        @Override
        public void afterViewChange(ViewChangeEvent event) {

        }
    });
}
}
public class SimpleLoginView extends CustomComponent implements View,Button.ClickListener {
private static final long serialVersionUID = 1L;

public static final String NAME = "login";

private final TextField user;

private final PasswordField password;

private final Button loginButton;

private final Button regButton;

public SimpleLoginView() {
    setSizeFull();

    // Create the user input field
    user = new TextField("User:");
    user.setWidth("300px");
    user.setRequired(true);
    user.setInputPrompt("Your username (eg. joe@email.com)");
    user.addValidator(new EmailValidator(
            "Username must be an email address"));
    user.setInvalidAllowed(false);

    // Create the password input field
    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    HorizontalLayout panel = new HorizontalLayout();
    // Create login button
    loginButton = new Button("Login", this);
    regButton = new Button("Register", this);
    panel.addComponent(loginButton);
    panel.addComponent(regButton);
    panel.setSpacing(true);

    // Add both to a panel
    VerticalLayout fields = new VerticalLayout(user, password, panel);
    fields.setCaption("Please login to access the application. (test@test.com/passw0rd)");
    fields.setSpacing(true);
    fields.setMargin(new MarginInfo(true, true, true, false));
    fields.setSizeUndefined();
    fields.setStyleName(Reindeer.LAYOUT_BLACK);


    // The view root layout
    VerticalLayout viewLayout = new VerticalLayout(fields);
    viewLayout.setSizeFull();
    viewLayout.setComponentAlignment(fields, Alignment.MIDDLE_CENTER);
    viewLayout.setStyleName(Reindeer.LAYOUT_BLACK);
    setCompositionRoot(viewLayout);
}

@Override
public void enter(ViewChangeEvent event) {
    // focus the username field when user arrives to the login view
    user.focus();
}

@Override
public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(loginButton))  {
        boolean validate=validateLogin();
        if (validate)   {
            getUI().getNavigator().navigateTo(MainView.NAME);
        }
        else    {
            Notification.show("Username/Password provided is incorrect");
        }
    }
    if (event.getButton().equals(regButton))    {
        System.out.println("Invoking Registration View");
        getUI().getNavigator().navigateTo(RegistrationView.NAME);
    }

};

public boolean validateLogin()  {
    if (!user.isValid() || !password.isValid()) {
        return false;
    }

    String username = user.getValue();
    String password = this.password.getValue();

    //
    // Validate username and password with database here. For examples sake
    // I use a dummy username and password.
    //
    boolean isValid = username.equals("test@test.com")
            && password.equals("passw0rd");

    if (isValid) {

        // Store the current user in the service session
        getSession().setAttribute("user", username);

        // Navigate to main view
        //
        return true;

    } else {

        // Wrong password clear the password field and refocuses it
        this.password.setValue(null);
        this.password.focus();
        return false;
    }
}
}
public class RegistrationView extends CustomComponent implements View,Button.ClickListener {
public static final String NAME = "Registration";
private static final long serialVersionUID = 1L;
private final Label feedback;
private final TextField username;
private final PasswordField password;
private final PasswordField verifyPassword;
private final TextField name;
private final TextField email;
private final Button registerBtn;
private final Button clearBtn;
private VerticalLayout fields = new VerticalLayout();
public RegistrationView() {

    feedback = new Label();

    username = new TextField("Registration");
    username.setWidth("300px");
    username.setNullRepresentation("");

    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    verifyPassword = new PasswordField("verifyPassword:");
    verifyPassword.setWidth("300px");
    verifyPassword.addValidator(new PasswordValidator());
    verifyPassword.setRequired(true);
    verifyPassword.setValue("");
    verifyPassword.setNullRepresentation("");

    name = new TextField("Name");
    name.setWidth("300px");
    name.setNullRepresentation("");

    email = new TextField("Email");
    email.setWidth("300px");
    email.setNullRepresentation("");
    email.setInputPrompt("Your email (eg. joe@email.com)");
    email.addValidator(new EmailValidator(
            "Email Address"));

    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    registerBtn = new Button("Register", this);
    buttonLayout.addComponent(registerBtn);

    clearBtn = new Button("Cancel", this);
    buttonLayout.addComponent(clearBtn);

    fields.addComponent(feedback);
    fields.addComponent(username);
    fields.addComponent(password);
    fields.addComponent(verifyPassword);
    fields.addComponent(name);
    fields.addComponent(email);
    fields.addComponent(buttonLayout);
    fields.setStyleName(Reindeer.LAYOUT_BLACK); 
}


public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(registerBtn)) {
        Notification.show("Registered user");
    }
    else if (event.getButton().equals(clearBtn)) {
        activated();
        Notification.show("Clear all the fields");
    }
}

public void activated() {
    username.setValue("");
    password.setValue("");
    verifyPassword.setValue("");
    name.setValue("");
    email.setValue("");
}

@Override
public void enter(ViewChangeEvent event) {
    // TODO Auto-generated method stub
    username.focus();
}
}
public class MainView extends CustomComponent implements View {
private static final long serialVersionUID = 1L;

public static final String NAME = "";

Label text = new Label();

Button logout = new Button("Logout", new Button.ClickListener() {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void buttonClick(ClickEvent event) {

        // "Logout" the user
        getSession().setAttribute("user", null);

        // Refresh this view, should redirect to login view
        getUI().getNavigator().navigateTo(NAME);
    }
});

public MainView() {
    setCompositionRoot(new CssLayout(text, logout));
}

@Override
public void enter(ViewChangeEvent event) {
    // Get the user name from the session
    String username = String.valueOf(getSession().getAttribute("user"));

    // And show the username
    text.setValue("Hello " + username);
}
}

2)登录视图

public class LoginUI extends UI {

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = LoginUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {

    //
    // Create a new instance of the navigator. The navigator will attach
    // itself automatically to this view.
    //
    new Navigator(this, this);

    //
    // The initial log view where the user can login to the application
    //
    getNavigator().addView(SimpleLoginView.NAME, SimpleLoginView.class);

    getNavigator().addView(RegistrationView.NAME, RegistrationView.class);
    //
    // Add the main view of the application
    //
    getNavigator().addView(MainView.NAME, MainView.class);

    //
    // We use a view change handler to ensure the user is always redirected
    // to the login view if the user is not logged in.
    //
    getNavigator().addViewChangeListener(new ViewChangeListener() {

        @Override
        public boolean beforeViewChange(ViewChangeEvent event) {

            // Check if a user has logged in
            System.out.println("getSession().getAttribute(\"user\") is " + getSession().getAttribute("user"));
            boolean isLoggedIn = getSession().getAttribute("user") != null;
            boolean isLoginView = event.getNewView() instanceof SimpleLoginView;

            System.out.println("isLoggedIn is " + isLoggedIn);
            System.out.println("isLoginView is " + isLoginView);

            if (!isLoggedIn && !isLoginView) {
                // Redirect to login view always if a user has not yet
                // logged in
                //getNavigator().navigateTo(SimpleLoginView.NAME);
                getNavigator().navigateTo(SimpleLoginView.NAME);
                return false;

            } else if (isLoggedIn && isLoginView) {
                // If someone tries to access to login view while logged in,
                // then cancel
                return false;
            }

            System.out.println("Returning True");
            return true;
        }

        @Override
        public void afterViewChange(ViewChangeEvent event) {

        }
    });
}
}
public class SimpleLoginView extends CustomComponent implements View,Button.ClickListener {
private static final long serialVersionUID = 1L;

public static final String NAME = "login";

private final TextField user;

private final PasswordField password;

private final Button loginButton;

private final Button regButton;

public SimpleLoginView() {
    setSizeFull();

    // Create the user input field
    user = new TextField("User:");
    user.setWidth("300px");
    user.setRequired(true);
    user.setInputPrompt("Your username (eg. joe@email.com)");
    user.addValidator(new EmailValidator(
            "Username must be an email address"));
    user.setInvalidAllowed(false);

    // Create the password input field
    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    HorizontalLayout panel = new HorizontalLayout();
    // Create login button
    loginButton = new Button("Login", this);
    regButton = new Button("Register", this);
    panel.addComponent(loginButton);
    panel.addComponent(regButton);
    panel.setSpacing(true);

    // Add both to a panel
    VerticalLayout fields = new VerticalLayout(user, password, panel);
    fields.setCaption("Please login to access the application. (test@test.com/passw0rd)");
    fields.setSpacing(true);
    fields.setMargin(new MarginInfo(true, true, true, false));
    fields.setSizeUndefined();
    fields.setStyleName(Reindeer.LAYOUT_BLACK);


    // The view root layout
    VerticalLayout viewLayout = new VerticalLayout(fields);
    viewLayout.setSizeFull();
    viewLayout.setComponentAlignment(fields, Alignment.MIDDLE_CENTER);
    viewLayout.setStyleName(Reindeer.LAYOUT_BLACK);
    setCompositionRoot(viewLayout);
}

@Override
public void enter(ViewChangeEvent event) {
    // focus the username field when user arrives to the login view
    user.focus();
}

@Override
public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(loginButton))  {
        boolean validate=validateLogin();
        if (validate)   {
            getUI().getNavigator().navigateTo(MainView.NAME);
        }
        else    {
            Notification.show("Username/Password provided is incorrect");
        }
    }
    if (event.getButton().equals(regButton))    {
        System.out.println("Invoking Registration View");
        getUI().getNavigator().navigateTo(RegistrationView.NAME);
    }

};

public boolean validateLogin()  {
    if (!user.isValid() || !password.isValid()) {
        return false;
    }

    String username = user.getValue();
    String password = this.password.getValue();

    //
    // Validate username and password with database here. For examples sake
    // I use a dummy username and password.
    //
    boolean isValid = username.equals("test@test.com")
            && password.equals("passw0rd");

    if (isValid) {

        // Store the current user in the service session
        getSession().setAttribute("user", username);

        // Navigate to main view
        //
        return true;

    } else {

        // Wrong password clear the password field and refocuses it
        this.password.setValue(null);
        this.password.focus();
        return false;
    }
}
}
public class RegistrationView extends CustomComponent implements View,Button.ClickListener {
public static final String NAME = "Registration";
private static final long serialVersionUID = 1L;
private final Label feedback;
private final TextField username;
private final PasswordField password;
private final PasswordField verifyPassword;
private final TextField name;
private final TextField email;
private final Button registerBtn;
private final Button clearBtn;
private VerticalLayout fields = new VerticalLayout();
public RegistrationView() {

    feedback = new Label();

    username = new TextField("Registration");
    username.setWidth("300px");
    username.setNullRepresentation("");

    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    verifyPassword = new PasswordField("verifyPassword:");
    verifyPassword.setWidth("300px");
    verifyPassword.addValidator(new PasswordValidator());
    verifyPassword.setRequired(true);
    verifyPassword.setValue("");
    verifyPassword.setNullRepresentation("");

    name = new TextField("Name");
    name.setWidth("300px");
    name.setNullRepresentation("");

    email = new TextField("Email");
    email.setWidth("300px");
    email.setNullRepresentation("");
    email.setInputPrompt("Your email (eg. joe@email.com)");
    email.addValidator(new EmailValidator(
            "Email Address"));

    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    registerBtn = new Button("Register", this);
    buttonLayout.addComponent(registerBtn);

    clearBtn = new Button("Cancel", this);
    buttonLayout.addComponent(clearBtn);

    fields.addComponent(feedback);
    fields.addComponent(username);
    fields.addComponent(password);
    fields.addComponent(verifyPassword);
    fields.addComponent(name);
    fields.addComponent(email);
    fields.addComponent(buttonLayout);
    fields.setStyleName(Reindeer.LAYOUT_BLACK); 
}


public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(registerBtn)) {
        Notification.show("Registered user");
    }
    else if (event.getButton().equals(clearBtn)) {
        activated();
        Notification.show("Clear all the fields");
    }
}

public void activated() {
    username.setValue("");
    password.setValue("");
    verifyPassword.setValue("");
    name.setValue("");
    email.setValue("");
}

@Override
public void enter(ViewChangeEvent event) {
    // TODO Auto-generated method stub
    username.focus();
}
}
public class MainView extends CustomComponent implements View {
private static final long serialVersionUID = 1L;

public static final String NAME = "";

Label text = new Label();

Button logout = new Button("Logout", new Button.ClickListener() {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void buttonClick(ClickEvent event) {

        // "Logout" the user
        getSession().setAttribute("user", null);

        // Refresh this view, should redirect to login view
        getUI().getNavigator().navigateTo(NAME);
    }
});

public MainView() {
    setCompositionRoot(new CssLayout(text, logout));
}

@Override
public void enter(ViewChangeEvent event) {
    // Get the user name from the session
    String username = String.valueOf(getSession().getAttribute("user"));

    // And show the username
    text.setValue("Hello " + username);
}
}

3)注册视图

public class LoginUI extends UI {

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = LoginUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {

    //
    // Create a new instance of the navigator. The navigator will attach
    // itself automatically to this view.
    //
    new Navigator(this, this);

    //
    // The initial log view where the user can login to the application
    //
    getNavigator().addView(SimpleLoginView.NAME, SimpleLoginView.class);

    getNavigator().addView(RegistrationView.NAME, RegistrationView.class);
    //
    // Add the main view of the application
    //
    getNavigator().addView(MainView.NAME, MainView.class);

    //
    // We use a view change handler to ensure the user is always redirected
    // to the login view if the user is not logged in.
    //
    getNavigator().addViewChangeListener(new ViewChangeListener() {

        @Override
        public boolean beforeViewChange(ViewChangeEvent event) {

            // Check if a user has logged in
            System.out.println("getSession().getAttribute(\"user\") is " + getSession().getAttribute("user"));
            boolean isLoggedIn = getSession().getAttribute("user") != null;
            boolean isLoginView = event.getNewView() instanceof SimpleLoginView;

            System.out.println("isLoggedIn is " + isLoggedIn);
            System.out.println("isLoginView is " + isLoginView);

            if (!isLoggedIn && !isLoginView) {
                // Redirect to login view always if a user has not yet
                // logged in
                //getNavigator().navigateTo(SimpleLoginView.NAME);
                getNavigator().navigateTo(SimpleLoginView.NAME);
                return false;

            } else if (isLoggedIn && isLoginView) {
                // If someone tries to access to login view while logged in,
                // then cancel
                return false;
            }

            System.out.println("Returning True");
            return true;
        }

        @Override
        public void afterViewChange(ViewChangeEvent event) {

        }
    });
}
}
public class SimpleLoginView extends CustomComponent implements View,Button.ClickListener {
private static final long serialVersionUID = 1L;

public static final String NAME = "login";

private final TextField user;

private final PasswordField password;

private final Button loginButton;

private final Button regButton;

public SimpleLoginView() {
    setSizeFull();

    // Create the user input field
    user = new TextField("User:");
    user.setWidth("300px");
    user.setRequired(true);
    user.setInputPrompt("Your username (eg. joe@email.com)");
    user.addValidator(new EmailValidator(
            "Username must be an email address"));
    user.setInvalidAllowed(false);

    // Create the password input field
    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    HorizontalLayout panel = new HorizontalLayout();
    // Create login button
    loginButton = new Button("Login", this);
    regButton = new Button("Register", this);
    panel.addComponent(loginButton);
    panel.addComponent(regButton);
    panel.setSpacing(true);

    // Add both to a panel
    VerticalLayout fields = new VerticalLayout(user, password, panel);
    fields.setCaption("Please login to access the application. (test@test.com/passw0rd)");
    fields.setSpacing(true);
    fields.setMargin(new MarginInfo(true, true, true, false));
    fields.setSizeUndefined();
    fields.setStyleName(Reindeer.LAYOUT_BLACK);


    // The view root layout
    VerticalLayout viewLayout = new VerticalLayout(fields);
    viewLayout.setSizeFull();
    viewLayout.setComponentAlignment(fields, Alignment.MIDDLE_CENTER);
    viewLayout.setStyleName(Reindeer.LAYOUT_BLACK);
    setCompositionRoot(viewLayout);
}

@Override
public void enter(ViewChangeEvent event) {
    // focus the username field when user arrives to the login view
    user.focus();
}

@Override
public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(loginButton))  {
        boolean validate=validateLogin();
        if (validate)   {
            getUI().getNavigator().navigateTo(MainView.NAME);
        }
        else    {
            Notification.show("Username/Password provided is incorrect");
        }
    }
    if (event.getButton().equals(regButton))    {
        System.out.println("Invoking Registration View");
        getUI().getNavigator().navigateTo(RegistrationView.NAME);
    }

};

public boolean validateLogin()  {
    if (!user.isValid() || !password.isValid()) {
        return false;
    }

    String username = user.getValue();
    String password = this.password.getValue();

    //
    // Validate username and password with database here. For examples sake
    // I use a dummy username and password.
    //
    boolean isValid = username.equals("test@test.com")
            && password.equals("passw0rd");

    if (isValid) {

        // Store the current user in the service session
        getSession().setAttribute("user", username);

        // Navigate to main view
        //
        return true;

    } else {

        // Wrong password clear the password field and refocuses it
        this.password.setValue(null);
        this.password.focus();
        return false;
    }
}
}
public class RegistrationView extends CustomComponent implements View,Button.ClickListener {
public static final String NAME = "Registration";
private static final long serialVersionUID = 1L;
private final Label feedback;
private final TextField username;
private final PasswordField password;
private final PasswordField verifyPassword;
private final TextField name;
private final TextField email;
private final Button registerBtn;
private final Button clearBtn;
private VerticalLayout fields = new VerticalLayout();
public RegistrationView() {

    feedback = new Label();

    username = new TextField("Registration");
    username.setWidth("300px");
    username.setNullRepresentation("");

    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    verifyPassword = new PasswordField("verifyPassword:");
    verifyPassword.setWidth("300px");
    verifyPassword.addValidator(new PasswordValidator());
    verifyPassword.setRequired(true);
    verifyPassword.setValue("");
    verifyPassword.setNullRepresentation("");

    name = new TextField("Name");
    name.setWidth("300px");
    name.setNullRepresentation("");

    email = new TextField("Email");
    email.setWidth("300px");
    email.setNullRepresentation("");
    email.setInputPrompt("Your email (eg. joe@email.com)");
    email.addValidator(new EmailValidator(
            "Email Address"));

    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    registerBtn = new Button("Register", this);
    buttonLayout.addComponent(registerBtn);

    clearBtn = new Button("Cancel", this);
    buttonLayout.addComponent(clearBtn);

    fields.addComponent(feedback);
    fields.addComponent(username);
    fields.addComponent(password);
    fields.addComponent(verifyPassword);
    fields.addComponent(name);
    fields.addComponent(email);
    fields.addComponent(buttonLayout);
    fields.setStyleName(Reindeer.LAYOUT_BLACK); 
}


public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(registerBtn)) {
        Notification.show("Registered user");
    }
    else if (event.getButton().equals(clearBtn)) {
        activated();
        Notification.show("Clear all the fields");
    }
}

public void activated() {
    username.setValue("");
    password.setValue("");
    verifyPassword.setValue("");
    name.setValue("");
    email.setValue("");
}

@Override
public void enter(ViewChangeEvent event) {
    // TODO Auto-generated method stub
    username.focus();
}
}
public class MainView extends CustomComponent implements View {
private static final long serialVersionUID = 1L;

public static final String NAME = "";

Label text = new Label();

Button logout = new Button("Logout", new Button.ClickListener() {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void buttonClick(ClickEvent event) {

        // "Logout" the user
        getSession().setAttribute("user", null);

        // Refresh this view, should redirect to login view
        getUI().getNavigator().navigateTo(NAME);
    }
});

public MainView() {
    setCompositionRoot(new CssLayout(text, logout));
}

@Override
public void enter(ViewChangeEvent event) {
    // Get the user name from the session
    String username = String.valueOf(getSession().getAttribute("user"));

    // And show the username
    text.setValue("Hello " + username);
}
}

4)主视图

public class LoginUI extends UI {

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = LoginUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {

    //
    // Create a new instance of the navigator. The navigator will attach
    // itself automatically to this view.
    //
    new Navigator(this, this);

    //
    // The initial log view where the user can login to the application
    //
    getNavigator().addView(SimpleLoginView.NAME, SimpleLoginView.class);

    getNavigator().addView(RegistrationView.NAME, RegistrationView.class);
    //
    // Add the main view of the application
    //
    getNavigator().addView(MainView.NAME, MainView.class);

    //
    // We use a view change handler to ensure the user is always redirected
    // to the login view if the user is not logged in.
    //
    getNavigator().addViewChangeListener(new ViewChangeListener() {

        @Override
        public boolean beforeViewChange(ViewChangeEvent event) {

            // Check if a user has logged in
            System.out.println("getSession().getAttribute(\"user\") is " + getSession().getAttribute("user"));
            boolean isLoggedIn = getSession().getAttribute("user") != null;
            boolean isLoginView = event.getNewView() instanceof SimpleLoginView;

            System.out.println("isLoggedIn is " + isLoggedIn);
            System.out.println("isLoginView is " + isLoginView);

            if (!isLoggedIn && !isLoginView) {
                // Redirect to login view always if a user has not yet
                // logged in
                //getNavigator().navigateTo(SimpleLoginView.NAME);
                getNavigator().navigateTo(SimpleLoginView.NAME);
                return false;

            } else if (isLoggedIn && isLoginView) {
                // If someone tries to access to login view while logged in,
                // then cancel
                return false;
            }

            System.out.println("Returning True");
            return true;
        }

        @Override
        public void afterViewChange(ViewChangeEvent event) {

        }
    });
}
}
public class SimpleLoginView extends CustomComponent implements View,Button.ClickListener {
private static final long serialVersionUID = 1L;

public static final String NAME = "login";

private final TextField user;

private final PasswordField password;

private final Button loginButton;

private final Button regButton;

public SimpleLoginView() {
    setSizeFull();

    // Create the user input field
    user = new TextField("User:");
    user.setWidth("300px");
    user.setRequired(true);
    user.setInputPrompt("Your username (eg. joe@email.com)");
    user.addValidator(new EmailValidator(
            "Username must be an email address"));
    user.setInvalidAllowed(false);

    // Create the password input field
    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    HorizontalLayout panel = new HorizontalLayout();
    // Create login button
    loginButton = new Button("Login", this);
    regButton = new Button("Register", this);
    panel.addComponent(loginButton);
    panel.addComponent(regButton);
    panel.setSpacing(true);

    // Add both to a panel
    VerticalLayout fields = new VerticalLayout(user, password, panel);
    fields.setCaption("Please login to access the application. (test@test.com/passw0rd)");
    fields.setSpacing(true);
    fields.setMargin(new MarginInfo(true, true, true, false));
    fields.setSizeUndefined();
    fields.setStyleName(Reindeer.LAYOUT_BLACK);


    // The view root layout
    VerticalLayout viewLayout = new VerticalLayout(fields);
    viewLayout.setSizeFull();
    viewLayout.setComponentAlignment(fields, Alignment.MIDDLE_CENTER);
    viewLayout.setStyleName(Reindeer.LAYOUT_BLACK);
    setCompositionRoot(viewLayout);
}

@Override
public void enter(ViewChangeEvent event) {
    // focus the username field when user arrives to the login view
    user.focus();
}

@Override
public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(loginButton))  {
        boolean validate=validateLogin();
        if (validate)   {
            getUI().getNavigator().navigateTo(MainView.NAME);
        }
        else    {
            Notification.show("Username/Password provided is incorrect");
        }
    }
    if (event.getButton().equals(regButton))    {
        System.out.println("Invoking Registration View");
        getUI().getNavigator().navigateTo(RegistrationView.NAME);
    }

};

public boolean validateLogin()  {
    if (!user.isValid() || !password.isValid()) {
        return false;
    }

    String username = user.getValue();
    String password = this.password.getValue();

    //
    // Validate username and password with database here. For examples sake
    // I use a dummy username and password.
    //
    boolean isValid = username.equals("test@test.com")
            && password.equals("passw0rd");

    if (isValid) {

        // Store the current user in the service session
        getSession().setAttribute("user", username);

        // Navigate to main view
        //
        return true;

    } else {

        // Wrong password clear the password field and refocuses it
        this.password.setValue(null);
        this.password.focus();
        return false;
    }
}
}
public class RegistrationView extends CustomComponent implements View,Button.ClickListener {
public static final String NAME = "Registration";
private static final long serialVersionUID = 1L;
private final Label feedback;
private final TextField username;
private final PasswordField password;
private final PasswordField verifyPassword;
private final TextField name;
private final TextField email;
private final Button registerBtn;
private final Button clearBtn;
private VerticalLayout fields = new VerticalLayout();
public RegistrationView() {

    feedback = new Label();

    username = new TextField("Registration");
    username.setWidth("300px");
    username.setNullRepresentation("");

    password = new PasswordField("Password:");
    password.setWidth("300px");
    password.addValidator(new PasswordValidator());
    password.setRequired(true);
    password.setValue("");
    password.setNullRepresentation("");

    verifyPassword = new PasswordField("verifyPassword:");
    verifyPassword.setWidth("300px");
    verifyPassword.addValidator(new PasswordValidator());
    verifyPassword.setRequired(true);
    verifyPassword.setValue("");
    verifyPassword.setNullRepresentation("");

    name = new TextField("Name");
    name.setWidth("300px");
    name.setNullRepresentation("");

    email = new TextField("Email");
    email.setWidth("300px");
    email.setNullRepresentation("");
    email.setInputPrompt("Your email (eg. joe@email.com)");
    email.addValidator(new EmailValidator(
            "Email Address"));

    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.setSpacing(true);
    registerBtn = new Button("Register", this);
    buttonLayout.addComponent(registerBtn);

    clearBtn = new Button("Cancel", this);
    buttonLayout.addComponent(clearBtn);

    fields.addComponent(feedback);
    fields.addComponent(username);
    fields.addComponent(password);
    fields.addComponent(verifyPassword);
    fields.addComponent(name);
    fields.addComponent(email);
    fields.addComponent(buttonLayout);
    fields.setStyleName(Reindeer.LAYOUT_BLACK); 
}


public void buttonClick(ClickEvent event) {
    if (event.getButton().equals(registerBtn)) {
        Notification.show("Registered user");
    }
    else if (event.getButton().equals(clearBtn)) {
        activated();
        Notification.show("Clear all the fields");
    }
}

public void activated() {
    username.setValue("");
    password.setValue("");
    verifyPassword.setValue("");
    name.setValue("");
    email.setValue("");
}

@Override
public void enter(ViewChangeEvent event) {
    // TODO Auto-generated method stub
    username.focus();
}
}
public class MainView extends CustomComponent implements View {
private static final long serialVersionUID = 1L;

public static final String NAME = "";

Label text = new Label();

Button logout = new Button("Logout", new Button.ClickListener() {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void buttonClick(ClickEvent event) {

        // "Logout" the user
        getSession().setAttribute("user", null);

        // Refresh this view, should redirect to login view
        getUI().getNavigator().navigateTo(NAME);
    }
});

public MainView() {
    setCompositionRoot(new CssLayout(text, logout));
}

@Override
public void enter(ViewChangeEvent event) {
    // Get the user name from the session
    String username = String.valueOf(getSession().getAttribute("user"));

    // And show the username
    text.setValue("Hello " + username);
}
}

如果用户尚未登录,则此评论是否总是重定向到登录视图基本上解释了您的问题?是的,我的错,谢谢。。但是我有问题。。我需要添加以下条件。布尔值isregView=event.getNewView()instanceof RegistrationView;