我正在尝试遵循Play2.0 JavaToDO教程,但由于某种原因它只是不想工作。浏览了 stackoverflow 和其他在线资源,但没有找到答案,这让我发疯。
Application.java的附加代码
package controllers;
import models.Task;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
public class Application extends Controller {
static Form<Task> taskForm = form(Task.class);
public static Result index() {
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(
views.html.index.render(Task.all(), taskForm));
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
附上Task java的代码
package models;
import java.util.List;
import javax.persistence.Entity;
import play.data.Form;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model.Finder;
import play.mvc.Result;
import controllers.routes;
@Entity
public class Task {
public Long id;
@Required
public String label;
// search
public static Finder<Long,Task> find = new Finder(
Long.class, Task.class);
// display tasks
public static List<Task> all() {
return find.all();
}
// create task
public static void create(Task task) {
task.create(task);
}
// delete task
public static void delete(Long id) {
find.ref(id).delete(id);
// find.ref(id).delete();
}
// create new task
public static Result newTask() {
Form<Task> filledForm = taskForm.bindFromRequest();
if(filledForm.hasErrors()) {
return badRequest(
views.html.index.render(Task.all(), filledForm)
);
} else {
Task.create(filledForm.get());
return redirect(routes.Application.tasks());
}
}
}
我在 Task.java 上得到一个编译错误
static Form<Task> taskForm = form(Task.class);
当我在 eclipse 上工作时(该项目在导入之前被 eclipsified),它告诉我 taskForm 无法解析,它还强调了每个 play 2 命令,例如“render()、redirect()、bindFromRequest()”要求我创建一种方法。任何想法如何解决编译错误以及如何让 Eclipse 识别 play2 命令?
编辑:
更新了 Application.java
package controllers;
import models.Task;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
public class Application extends Controller {
// create new task
public static Result newTask() {
Form<Task> filledForm = form(Task.class).bindFromRequest();
if(filledForm.hasErrors()) {
return badRequest(
views.html.index.render(Task.all(), filledForm)
);
} else {
Task.newTask(filledForm.get());
return redirect(routes.Application.tasks());
}
}
public static Result index() {
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(
views.html.index.render(Task.all(), taskForm));
}
public static Result deleteTask(Long id) {
return TODO;
}
}
更新了 task.java
package models;
import java.util.List;
import javax.persistence.Entity;
import play.data.Form;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
import play.db.ebean.Model.Finder;
import play.mvc.Result;
import controllers.routes;
@Entity
public class Task extends Model {
public Long id;
@Required
public String label;
// Define a taskForm
static Form<Task> taskForm = form(Task.class);
// search
public static Finder<Long,Task> find = new Finder(
Long.class, Task.class);
// display tasks
public static List<Task> all() {
return find.all();
}
// create new task
public static Result newTask(Task newTask) {
save(task);
}
// delete task
public static void delete(Long id) {
find.ref(id).delete(id);
// find.ref(id).delete();
}
}