【问题标题】:EBean : Why does bindFromRequest() return null values?EBean:为什么 bindFromRequest() 返回空值?
【发布时间】:2014-08-10 14:58:21
【问题描述】:

我正在使用带有 Sqlite 的 Play 框架。

我创建了一个模型 - 酒吧

(app/models/Bar.java 的内容)

package models;

import javax.persistence.Entity;
import javax.persistence.Id;
import play.db.ebean.Model;

@Entity
public class Bar extends Model{

    @Id
    public String id;
    public String name;

    public Bar(String id, String name){
        this.id = id;
        this.name = name;

    }
    public Bar(){}

    public static Finder<String, Bar> find = new Finder(String.class, Bar.class);

}

我正在尝试通过表单提交添加栏:

public static Result addBar(){
    Form<Bar> form = Form.form(Bar.class).bindFromRequest();
    Bar bar = form.get();
    bar.save();
    return redirect(routes.Application.index());

}

但是,这里 bar 中的 id 和 name 都是空的。 请帮助我理解为什么会这样? 谢谢。

【问题讨论】:

  • ebean.default 在您的application.conf 文件中的值是多少?
  • ebean.default="models.*"
  • 然后尝试将Bar的包重命名为models而不是model
  • @Chafik 做到了。不再出现该错误:) 但是 form.get() 具有空值!我在这里还缺少什么?
  • 我做了一个完整的答案,如果它解决了你的问题,你可以接受它

标签: java playframework-2.0 persistence ebean


【解决方案1】:

为未来的用户回答这个问题:

如果在application.conf 文件中,Ebean 配置为ebean.default="models.*",则包含模型的包必须为models 而不是model

关于另一个问题(form.get() 为空),在从请求中绑定您的表单后,您需要确保它没有错误:

public static Result addBar() {
    Form<Bar> form = Form.form(Bar.class).bindFromRequest();
    if (form.hasErrors()) {
        // Do something with the errors (i.e. : redirect with flash message containing the main error)
    }
    Bar bar = form.get();
    bar.save();
    return redirect(routes.Application.index());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多