我正在使用链接https://vaadin.com/wiki/-/wiki/Main/Creating+a+simple+login+view
为Prototype构建我的新Vaadin应用程序我有3个观看次数
1)登录视图 2)主视图 3)注册视图
和
1)处理导航的基类LoginUI.java。
所以,总而言之,我有一个登录视图,它是主登录屏幕,有2个按钮,一个用于登录应用程序,另一个用于注册新用户。
并且这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 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) {
}
});
}
}
2)登录视图
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;
}
}
}
3)注册视图
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;
}
}
}
4)主视图
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 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();
}
}