在BindRequest上播放框架错误

时间:2013-08-08 03:21:50

标签: java controller playframework-2.0

我在Play 2.1.3上遇到以下错误: [RuntimeException:无法实例化类controllers.Application $ User。它必须有一个默认的构造函数]

这是我的Application.java文件......我不知道我做错了什么!当我向/ login发出POST请求时会发生这种情况。

package controllers;

import play.mvc.*;
import play.data.*;
import views.html.*;

public class Application extends Controller {

    private static boolean loggedin = false;
    private static User currentUser;
    private static Form<User> LoginForm = Form.form(User.class);

    public static Result index() {

        String message, title;

        if(!loggedin)
        {
            title = "Login";
            message = "Please log in.";
        }
        else
        {
            title = "Welcome";
            message = "You are logged in!";
        }
        return ok(index.render(title, message, loggedin));
    }

    public static Result login() {

        currentUser = LoginForm.bindFromRequest().get();

        if(currentUser.getUsername().equals("test") && currentUser.getPassword().equals("password")) {
            loggedin = true;
            return redirect(routes.Application.index());
        }
        else {
            String title = "Login";
            String message = "An error occurred. Please try logging in again.";
            return ok(index.render(title, message, loggedin));
        }
    }

    static class User {
        private String username;
        private String password;

        public User() {
            this.username = "";
            this.password = "";
        }

        public String getUsername() {
            return this.username;
        }
        public String getPassword() {
            return this.password;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您的User类需要具有默认构造函数。通过提供没有正式参数的构造函数,您已覆盖默认构造函数,因此在编译时不会生成一个。尝试删除构造函数,看看它是否解决了问题。