在BindRequest上播放框架错误 [英] Play Framework Error on BindRequest

查看:184
本文介绍了在BindRequest上播放框架错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

}


推荐答案

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

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.

这篇关于在BindRequest上播放框架错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆