Playframework(6)Java Project and TODO Sample

本文档通过创建一个简单的 Java TodoList 应用程序来介绍 Playframework 的基本使用方法。文章详细介绍了如何从创建项目开始,到配置数据库、定义路由、实现控制器、模板渲染以及模型持久化等全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值