I'm trying to follow the Play2.0 JavaToDO tutorial and for some reason it just doesn't want to work. Have looked through stackoverflow and other online resources, but haven't find an answer to this and it's driving me crazy.
Attached code of the 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;
}
}
Attached code of the 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());
}
}
}
I get a compile error on Task.java on the line
static Form<Task> taskForm = form(Task.class);
As I'm working on eclipse (the project is eclipsified before import), it's telling me that taskForm cannot be resolved and it also underlines every play 2 command e.g. "render(), redirect(), bindFromRequest()" asking me to create a method for it. Any ideas how to solve the compilations error and also how to get Eclipse to recognize the play2 commands?
EDIT:
updated 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;
}
}
Updated 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();
}
}