Java Play 2.3.8:DataSource用户是否为空? bindFromRequest()无法正常工作

时间:2015-03-21 11:24:09

标签: java html eclipse scala playframework-2.0

本周我开始学习Play。现在我正在尝试编写一个用户登录时创建会话的代码。 对不起,但是帖子看起来有点乱,因为我是新用户,不允许发布图片。

我正在使用Java play 2.3.8和eclipse 4.4.1(我已经尝试了我在这里看到的其他技巧)

目前我总是收到此错误消息:

  

play.api.Application $$ anon $ 1:执行异常[[RuntimeException:java.lang.reflect.InvocationTargetException]]       at play.api.Application $ class.handleError(Application.scala:296)〜[play_2.11-2.3.6.jar:2.3.6]       at play.api.DefaultApplication.handleError(Application.scala:402)[play_2.11-2.3.6.jar:2.3.6]       at play.core.server.netty.PlayDefaultUpstreamHandler $$ anonfun $ 3 $$ anonfun $ applyOrElse $ 4.apply(PlayDefaultUpstreamHandler.scala:320)[play_2.11-2.3.6.jar:2.3.6]       at play.core.server.netty.PlayDefaultUpstreamHandler $$ anonfun $ 3 $$ anonfun $ applyOrElse $ 4.apply(PlayDefaultUpstreamHandler.scala:320)[play_2.11-2.3.6.jar:2.3.6]       在scala.Option.map(Option.scala:145)[scala-library-2.11.1.jar:na]   引起:java.lang.RuntimeException:java.lang.reflect.InvocationTargetException       at play.data.Form.bind(Form.java:394)〜[play-java_2.11-2.3.6.jar:2.3.6]       at play.data.Form.bindFromRequest(Form.java:221)〜[play-java_2.11-2.3.6.jar:2.3.6]       在controllers.Application.authenticate(Application.java:70)〜[classes /:na]       在Routes $$ anonfun $ routes $ 1 $$ anonfun $ applyOrElse $ 11 $$ anonfun $ apply $ 11.apply(routes_routing.scala:209)〜[classes /:na]       在Routes $$ anonfun $ routes $ 1 $$ anonfun $ applyOrElse $ 11 $$ anonfun $ apply $ 11.apply(routes_routing.scala:209)〜[classes /:na]   引起:java.lang.reflect.InvocationTargetException:null       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_31]       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_31]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_31]       在java.lang.reflect.Method.invoke(Method.java:483)〜[na:1.8.0_31]       at play.data.Form.bind(Form.java:391)〜[play-java_2.11-2.3.6.jar:2.3.6]   引起:java.lang.RuntimeException:DataSource用户为空?       在com.avaje.ebeaninternal.server.lib.sql.DataSourcePool。(DataSourcePool.java:204)〜[avaje-ebeanorm-3.3.4.jar:na]       at com.avaje.ebeaninternal.server.core.DefaultServerFactory.getDataSourceFromConfig(DefaultServerFactory.java:419)~ [avaje-ebeanorm-3.3.4.jar:na]       at com.avaje.ebeaninternal.server.core.DefaultServerFactory.setDataSource(DefaultServerFactory.java:385)〜[avaje-ebeanorm-3.3.4.jar:na]       在com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:163)〜[avaje-ebeanorm-3.3.4.jar:na]       at com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:125)~ [avaje-ebeanorm-3.3.4.jar:na]

这是我的方法,名为:

public static Result authenticate() {

    Form<Login> loginForm = Form.form(Login.class);

    Login loginUser = loginForm.bindFromRequest().get();


    if (!usersList.getUsers().contains(loginForm)) {
        return ok(login.render("test"));
    } else {
        session().clear();
        session("email", loginForm.get().email);
        System.out.println("ich bin hier");
        return redirect(routes.Application.login());

    }


}

这是模型(getter和setter也在那里):

public class Login {

public Login(){

}
public String email;
public String password;

public Login(String email, String password){
    this.email=email;
    this.password=password;
}

public String validate() {
    if (Users.authenticate(email, password) == null) {
      return "Invalid user or password";
    }
    return null;
}

public static Finder<String,Login> find = new Finder<String,Login>(
        String.class, Login.class
    ); 

这里是scala.html文档:

<div class="Login">
   <br>
    <br>
     <br>
      <br>

      <h1>Login</h1>

<form action="@routes.Application.authenticate()" method="post">
 <p><input type="email" name="email" placeholder="Email" value=""></p>
 <p><input type="password" name="password" placeholder="Password" value=""></p>
 <p class="submit"><input type="submit" name="Login" value="Login"></p>
</form>   

最后但并非最不重要的是,我的浏览器中的错误消息说第70行引发了异常:

  

[RuntimeException:java.lang.reflect.InvocationTargetException]

     

System.out.println(&#34; nach loginform&#34;);   68   69 //System.out.println(loginForm.bindFromRequest()。toString());   70登录loginUser = loginForm.bindFromRequest()。get();   71   72 System.out.println(loginForm.toString());   73 //表单loginForm = form(Login.class).bindFromRequest();   74个
  75 if(!usersList.getUsers()。contains(loginForm)){

提前感谢帮助我解决这个问题

1 个答案:

答案 0 :(得分:2)

你遗失了什么

@Entity 
public class Login extends Model {

并检查ebean.default="models.*"

中是否取消注释application.conf