我今天开始学习Play框架,它非常好,很容易学。
我已经完成了他们网站上提供的样品,但是我想做一些修改。
我想看看是否可以更新一个特定任务的标签,所以我采用了以下方法
首先,我添加了一个路由来更新数据
POST /tasks/:id/update controllers.Application.updateTask(id: Long)
然后我将以下代码添加到index.scala.html
文件
@form(routes.Application.updateTask(task.id)) {
<label class="contactLabel">Update note here:</label>
@inputText(taskForm("label")) <br />
}
然后我修改Application.java类为
public static Result updateTask(Long id) {
Form<Task> taskForm = Form.form(Task.class).bindFromRequest();
if (taskForm.hasErrors()) {
return badRequest(views.html.index.render(Task.all(), taskForm));
} else {
Task.update(id, taskForm.get());
return redirect(routes.Application.tasks());
}
}
最后在Task.java中我添加了以下代码
public static void update(Long id, Task task) {
find.ref(id).update(task.label);
}
但是当我执行更新操作时,我得到了这个错误
[RuntimeException: DataSource user is null?])
不用说我把
注释掉了 db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"
在application.conf,因为我已经能够保存和删除数据;但是我无法更新数据库中的数据,为什么会发生这种情况,之前有人尝试过吗,我该如何解决这个错误?
Task
模型上的update(Long id, Task task)
方法应该如下所示:
public static void update(Long id, Task task) {
task.update(id); // updates this entity, by specifying the entity ID
}
因为你传递了task
变量作为更新的数据,你不需要像在find.ref(id)
中那样找到Task
对象的引用。play.db.ebean.Model
类(单参数)上的update()
方法需要模型的ID作为参数。
希望这有助于解决你的问题。:)
确保您已经注释掉application.conf中的Ebean配置
# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
#
ebean.default="models.*"