Play Framework Error on BindRequest

一个人想着一个人 提交于 2019-12-11 07:55:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!