日期:2014-05-20  浏览次数:21035 次

Playframework(6)Java Project and TODO Sample
Playframework(6)Java Project and TODO Sample
Create Project
>play new todolist
I choose simple java project.
>cd todolist
>play eclipsify
Make it works in eclipse.

Using the Play Console
>play
play>run

Overview and Preparing the application
Prepare the routes first, and redirect the home page to my working tasks.
The original Action is passing one parameter and send to scala html template
return ok(index.render("You new application is ready"));

The index scala html will template from the main template, pass the title and html to main
@(message: String)
@main("Welcome to Play 2.0"){
     @play20.welcome(message)
}

First define our entries in routes
#Tasks
GET       /tasks               controllers.Application.tasks()
POST      /tasks               controllers.Application.newTask()
POST      /tasks/:id/delete    controllers.Application.deleteTask(id: Long)

We can add the action in controllers, and we can return TODO instead of Result at first.

And it will return 501 Not Implemented response

Change the home page to redirect to my tasks
return redirect(routes.Application.tasks());

Prepare the Task model
package models;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
@Entity
publicclass Task extends Model{

privatestaticfinallongserialVersionUID = -7161351873652106184L;

@Id
public Long id;

@Required
public String label;

publicstatic Finder<Long,Task> find = new Finder<Long, Task>(
Long.class, Task.class
);

publicstatic List<Task> all(){
returnfind.all();
}

publicstaticvoid create(Task task){
task.save();
}

publicstaticvoid delete(Long id){
find.ref(id).delete();
}

}

We use Ebean, and we use @Required, @Entity in our business model.

The application template
@(tasks: List[Task], taskForm: Form[Task])

@import helper._

@main("Todo List"){
  <h1]]>@tasks.size() task(s)</h1>
    <ul>
        @for(task <- tasks) {
            <li>
                @task.label
                @form(routes.Application.deleteTask(task.id)) {
                    <input type="submit" value="Delete">
                }
            </li>
        }
    </ul>
    <h2>Add a new task</h2>
    @form(routes.Application.newTask()) {
        @inputText(taskForm("label"))
        <input type="submit" value="Create">
    }
}

We use scala in our template, just use scala edit to open them.

The Task Form and Handling the form submission

In our action, we can get the form value from request
public class Application extends Controller {

static Form<Task> taskForm = form(Task.class);
…snip…
public static Result tasks() {
     // to do will return a 501 Not Implemented response
     //return to do; Capital it and get rid of the space
     return ok(views.html.index.render(Task.all(),taskForm));
     //this render method turned red, so I import SCALA 2.9.2, it works
}

publicstatic Result newTask() {
     //return to do;
     Form<Task> filledForm = taskForm.bindFromRequest();
     if(filledForm.hasErrors()){
          //return 400 Bad Request if we have errors.
          return badRequest(
          views.html.index.render(Task.all(),filledForm)
          );
     }else{
          Task.create(filledForm.get());
          return redirect(routes.Application.tasks());
     }
}


Persist the tasks in a database
Start to enable the database configuration in our application
>vi conf/application.conf
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"

Deleting Tasks
publicstatic Result deleteTask(Long id) {
     //return to do;
     Task.delete(id);
     return redirect(routes.Application.tasks());
}

Deploying to Heroku
I just ignore this. I do not want to deploy on that server.

Next step I will go on study some other samples. No, I will go on with SCALA then.

References:
http://www.playframework.org/documentation/2.0.4/JavaTodoList

http://sillycat.iteye.com/blog/1750340
http://sillycat.iteye.com/blog/1750947
http://sillycat.iteye.com/blog/1751649
http://sillycat.iteye.com/blog/1752183

http://www.playframework.org/documentation/2.0.4/Samples

http://www.playframework.org/documentation/2.0.4/ScalaHome

http://www.playframework.org/documentation/1.0/gae