问题
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;
}
}
}
回答1:
Your User class is required to have a default constructor. By providing a constructor with no formal paramaters, you have overridden the default constructor, therefore one is not being generated at compile time. Try removing the constructor and see if it fixes the problem.
来源:https://stackoverflow.com/questions/18117355/play-framework-error-on-bindrequest