java play Framework 实现Todo案例(mysql数据库)

Todo案例非常简单,访问localhost:9000/tasks显示所有的任务,可以执行添加和删除操作。

项目文件目录如下:



安装play,请参照官网http://www.playframework.org/


新建项目:

终端切换到项目存放的文件夹,在终端输入:play new todo

填写项目名称,

选择项目类型为java

第一步ok~

进入到todo文件夹中,在终端输入 play run todo

项目启动后,在浏览器访问localhost:9000

下面开始对项目进行修改

下面是整个项目的主控制器:Application.java

package controllers;

import play.*;
import play.mvc.*;
import play.data.*;
import models.*;

import views.html.*;

public class Application extends Controller {
  
  //首页
  public static Result index() {
    return ok("welcome!");
  }

  //定义一个Form实例
 static Form<Task> taskForm = form(Task.class);

  //显示任务列表
  public static Result tasks(){
  	return ok(
  		views.html.index.render(Task.all(),taskForm)
  	);
  }

  //添加任务
  public static Result newTask(){
  	/**
  	 * Form.bindFormRequest()
  	 * 将请求的数据绑定到这个Form上--也就是表单提交的句柄
  	 * 返回值是填充了新数据的表单副本
  	 * 
  	 */
	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());
  	}

  } 

  //删除任务
  public static Result deleteTask(Long id){
  	Task.delete(id);
  	return redirect(routes.Application.tasks());
  }
  

 
}

下面是Task实体:

package models;

import java.util.*;
import play.db.ebean.*;
import play.data.validation.Constraints.*;

import javax.persistence.*;

@Entity
public class Task extends Model{
	@Id
	public Long Id;

	@Required
	public String things;

	//Helper for Ebean queries
	public static Finder<Long,Task> find = new Finder(Long.class, Task.class);

	public static List<Task> all(){
		return find.all();
	}

	public static void create(Task task){
		task.save();
	}

	public static void delete(Long id){
		find.ref(id).delete();
	}

	//getter|setter
	public void setId(Long Id){
		this.Id = Id;
	}
	public Long getId(){
		return this.Id;
	}

	public void setThings(String things){
		this.things = things;
	}

	public String getThings(){
		return this.things;
	}
	
}

修改index.scala.html为

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

@import helper._

@main("Todo list"){
	<h2>@tasks.size() task(s)</h2>

	<ul>
		@for(task <- tasks){
			<li>
				@task.things

				@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("things"))
		<input type="submit" value="Create"/>
	}
}

conf/routes(路由)文件:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index()

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)


# Tasks

GET    /tasks     controllers.Application.tasks()

POST   /tasks     controllers.Application.newTask()

POST   /tasks/:id/delete     controllers.Application.deleteTask(id: Long)
在conf/application.conf文件追加一下代码

#mysql

db.default.driver=com.mysql.jdbc.Driver  
db.default.url="jdbc:mysql://localhost:3306/todo"  
db.default.user="root"  
db.default.password="root"  
db.default.logStatements=true 
db.default.logStatements=true  

ebean.default = "models.*"   #设置ebean的默认数据源

如果你想配置多个数据库,可以参照http://blog.csdn.net/love_se/article/details/7848734




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值