2014-09-12 103 views
0

我是Vaadin UI开发新手,如果遇到任何愚蠢的错误,请原谅我。有人可以帮忙吗?Vaadin登录页面问题

我正在尝试使用Vaadin网站中提到的链接https://vaadin.com/wiki/-/wiki/Main/Creating+a+simple+login+view来构建登录页面。但我得到的错误 - com.vaadin.server.ServiceException:了java.lang.RuntimeException:java.lang.InstantiationException

有3类

1)登录查看 2)对于主视图 3)其中上述2视图

1)登录视图之间导航的一个主要类是如下:

public abstract 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; 

public SimpleLoginView() { 
    setSizeFull(); 

    // Create the user input field 
    user = new TextField("User:"); 
    user.setWidth("300px"); 
    user.setRequired(true); 
    user.setInputPrompt("Your username (eg. [email protected])"); 
    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(""); 

    // Create login button 
    loginButton = new Button("Login", this); 

    // Add both to a panel 
    VerticalLayout fields = new VerticalLayout(user, password, loginButton); 
    fields.setCaption("Please login to access the application. ([email protected]/passw0rd)"); 
    fields.setSpacing(true); 
    fields.setMargin(new MarginInfo(true, true, true, false)); 
    fields.setSizeUndefined(); 

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

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

// Validator for validating the passwords 
private static final class PasswordValidator extends 
AbstractValidator<String> { 

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

    public PasswordValidator() { 
     super("The password provided is not valid"); 
    } 

    @Override 
    protected boolean isValidValue(String value) { 
     // 
     // Password must be at least 8 characters long and contain at least 
     // one number 
     // 
     if (value != null 
       && (value.length() < 8 || !value.matches(".*\\d.*"))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public Class<String> getType() { 
     return String.class; 
    } 
} 

@Override 
public void buttonClick(ClickEvent event) { 

    // 
    // Validate the fields using the navigator. By using validors for the 
    // fields we reduce the amount of queries we have to use to the database 
    // for wrongly entered passwords 
    // 
    if (!user.isValid() || !password.isValid()) { 
     return; 
    } 

    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("[email protected]") 
      && password.equals("passw0rd"); 

    if (isValid) { 

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

     // Navigate to main view 
     getUI().getNavigator().navigateTo(MainView.NAME);// 

    } else { 

     // Wrong password clear the password field and refocuses it 
     this.password.setValue(null); 
     this.password.focus(); 

    } 
} 

2)主视图是如下:

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),该导航主类之间上述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);// 

    // 
    // 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 
      boolean isLoggedIn = getSession().getAttribute("user") != null; 
      boolean isLoginView = event.getNewView() instanceof SimpleLoginView; 

      if (!isLoggedIn && !isLoginView) { 
       // Redirect to login view always if a user has not yet 
       // logged in 
       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; 
      } 

      return true; 
     } 

     @Override 
     public void afterViewChange(ViewChangeEvent event) { 

     } 
    }); 
} 

}

当运行在Tomcat我得到以下错误的代码:

`type Exception report

message com.vaadin.server.ServiceException: java.lang.RuntimeException:  java.lang.InstantiationException 

description The server encountered an internal error that prevented it from fulfilling this request. exception

javax.servlet.ServletException: com.vaadin.server.ServiceException: java.lang.RuntimeException: java.lang.InstantiationException com.vaadin.server.VaadinServlet.service(VaadinServlet.java:307) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

com.vaadin.server.ServiceException: java.lang.RuntimeException: java.lang.InstantiationException com.vaadin.server.VaadinService.handleExceptionDuringRequest(VaadinService.java:1460) com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1414) com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.RuntimeException: java.lang.InstantiationException com.vaadin.navigator.Navigator$ClassBasedViewProvider.getView(Navigator.java:340) com.vaadin.navigator.Navigator.navigateTo(Navigator.java:512) com.example.login.LoginUI$1.beforeViewChange(LoginUI.java:58) com.vaadin.navigator.Navigator.fireBeforeViewChange(Navigator.java:595) com.vaadin.navigator.Navigator.navigateTo(Navigator.java:553) com.vaadin.navigator.Navigator.navigateTo(Navigator.java:526) com.vaadin.ui.UI.doInit(UI.java:644) com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222) com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74) com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41) com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402) com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.InstantiationException sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect.Constructor.newInstance(Unknown Source) java.lang.Class.newInstance(Unknown Source) com.vaadin.navigator.Navigator$ClassBasedViewProvider.getView(Navigator.java:336) com.vaadin.navigator.Navigator.navigateTo(Navigator.java:512) com.example.login.LoginUI$1.beforeViewChange(LoginUI.java:58) com.vaadin.navigator.Navigator.fireBeforeViewChange(Navigator.java:595) com.vaadin.navigator.Navigator.navigateTo(Navigator.java:553) com.vaadin.navigator.Navigator.navigateTo(Navigator.java:526) com.vaadin.ui.UI.doInit(UI.java:644) com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222) com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74) com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41) com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402) com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.55 logs.


`

+0

您应该重新格式化您的问题,以便它可以正确阅读。 – 2014-09-12 13:28:28

回答

2

Vaadin的Navigator类尝试通过重新实例化其视图类如果通过Navigator#addView(String viewName, Class<? extends View> viewClass)将视图添加到导航器中,则会产生反感。但在你的情况下,类SimpleLoginView不能实例化,因为你声明它abstract。从您的视图类定义中删除分类器abstract,您的示例应该可以工作。

+0

完美! 非常感谢。它的工作原理:) – 2014-09-12 13:30:22

+0

一个问题,在SimpleLoginView.java中有一行如下 viewLayout.setStyleName(Reindeer.LAYOUT_BLUE); 我相信这应该将背景颜色设置为蓝色,但目前它没有这样做。它没有任何背景颜色。 请让我知道如何实现它? – 2014-09-12 13:34:51

+1

为了改变Vaadin应用程序的外观和感觉,我建议你看看Vaadin的主题:https://vaadin.com/book//page/themes.html这个主题太宽泛了,在评论中处理:) – 2014-09-12 13:42:29