1

I'm getting the following error on Play 2.1.3: [RuntimeException: Cannot instantiate class controllers.Application$User. It must have a default constructor]

Here's my Application.java file...I can't tell what I'm doing wrong! This occurs when I make a POST request to /login.

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;
        }
    }

}
4

1 回答 1

1

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

于 2013-08-08T09:35:58.367 回答