所有章节的翻译到此结束,实现一个小应用作为结束!
按照play2.0官网的例子编写第一个play Application : todolist
步骤如下(详细的步骤及解释请查看原文):
先编写控制器如下:
aplication.java
package controllers;
import play.*;
import play.data.*;
import play.mvc.*;
import models.*;
import views.html.*;
public class Application extends Controller {
static Form<Task> taskForm = form(Task.class);
public static Result index() {
//return ok(index.render("Your new application is ready."));
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(
views.html.index.render(Task.all(), taskForm)
);
}
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());
}
}
public static Result deleteTask(Long id) {
Task.delete(id);
return redirect(routes.Application.tasks());
}
}
修改routes:
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.Application.index()
# Tasks
GET /tasks controllers.Application.tasks()
POST /tasks controllers.Application.newTask()
POST /tasks/:id/delete controllers.Application.deleteTask(id: Long)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
编写一个model(用到了Ebean,需要在application.conf中配置):
Task.java
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 label;
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();
}
}
编写views:
main.scala.html
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>
index.scala.html
@(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">
}
}
最后改一下配置:
# This is the main configuration file for the application.
# ~~~~~
# Secret key
# ~~~~~
# The secret key is used to secure cryptographics functions.
# If you deploy your application to several instances be sure to use the same key!
application.secret="@8DeN:VCjdHS7g2Y=L0et:x/e8DseMHvPLvEnYqRy@R[o2V[f547@wyV/o^K@56i"
# The application languages
# ~~~~~ 中文
application.langs="zh"
# Global object class
# ~~~~~
# Define the Global object class for this application.
# Default to Global in the root package.
# global=Global
# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#使用h2
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
# db.default.user=sa
# db.default.password=
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
# db.default.jndiName=DefaultDS
# Evolutions
# ~~~~~
# You can disable evolutions if needed
# evolutionplugin=disabled
# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
# 使用Ebean
ebean.default="models.*"
# Logger
# ~~~~~
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
# Root logger:
logger.root=ERROR
# Logger used by the framework:
logger.play=INFO
# Logger provided to your application:
logger.application=DEBUG
现在play run一下就可以看到效果了!
附件是程序源代码。
分享到:
相关推荐
Play framework 2.0入门教程(三)的源代码,Play留言板,详见http://blog.csdn.net/wfdztl521/article/details/8545838
Play framework 2.0入门教程(四)的源代码,Play留言板(数据库版),详见http://blog.csdn.net/wfdztl521/article/details/8766988
使用 Play-Framework 2.3.x 的 Scala Oauth 2.0 在这个示例应用程序中,我们使用Oauth2.0创建带有Play-Framework 2.3.x 、 ReactiveMongo-Extensions 、 Cake-Pattern和Scala-Oauth2-Provider 的API。 我们使用 ...
如何从一个应用程序里加载模块 - 115 - 从模块加载默认的routes - 115 - 为模块增加文档说明 - 115 - 使用模块仓库 - 116 - 贡献新模块到模块仓库里 - 117 - 先决条件 - 117 - 模块注册 - 117 - 发布你的模块 - 118 ...
play框架手册-15.测试应用程序[定义].pdf
play20-spring-demo, 在播放 2.0应用程序中使用 spring 在播放 2.0应用程序中使用 spring的这是一个简单的应用程序演示如何将播放 2.0应用程序组件与 Spring Framework 应用程序集成。请注意,同样的技术可以应用到...
更新播放 2.4.0添加测试规范Playframework 2.2.1 (Scala) 和 MongoDb (Salat) 的第一步使用 MongoDb 而不是 Anorm 的待办事项应用程序我们将为 MongoDb 使用 Salat 库( )。 Play 2 有一个替代的“mongo-jackson-...
Playframework 2.3.1 (Java) 和 MongoDb (Jackson) 的第一步 不要忘记启动mongodb守护进程! 它应该使用默认主机 (localhost) 和默认端口。 运行应用程序: sbt run 如果运行本教程有任何问题,请报告,谢谢!
使用Play Framework 2.0 +,jQuery,Backbone.js,Marionette.js和Postgresql用Java / Scala编写的ERP应用程序 开发人员虚拟机 您可以下载此zip文件,其中包含安装并配置了Ubuntu 16.04,PostgreSQL,PlayFramework...
将Elasticsearch集成到您的PlayFramework应用中的模块。安装该模块需要PlayFramework 2.4。 克隆此模块并将其添加到本地存储库。 $ git clone https://github.com/eduardofcbg/playframework2-elasticsearch.git$ cd...
play2.0 for java developers.pdf
如何从一个应用程序里加载模块 - 115 - 从模块加载默认的routes - 115 - 为模块增加文档说明 - 115 - 使用模块仓库 - 116 - 贡献新模块到模块仓库里 - 117 - 先决条件 - 117 - 模块注册 - 117 - 发布你的模块 - 118 ...
TLeague_ A Framework for Competitive Self-Play basedDistributed Multi-Agent RL
虽然许多公共数据集(如Kaggle等)提供苹果App Store数据,但很少有对应的数据集可用于网络上任何地方的Google Play Store应用程序。在深入挖掘后,我发现iTunes App Store页面部署了一个很好的索引附录结构,允许简单...
google-play-services版本15.0.1,依赖工程可直接引用到项目中,适用Eclipse及AndroidStudio开发环境,快捷方便,本人亲自整理,后续资源有问题可咨询。若后续google-play-services如需其他版本迭代,也可让我更进。
play2-war-plugin, 用于 Play Framework 2.x的WAR插件 用于 Play Framework 2.x的 WAR插件Current versions: Play 2.2.x : 1.2.1 Play 2.3.0 -> 2.3.1 : 1.3-beta1 (Scala 2.1
play手把手教你创建一个博客项目-10完整的应用程序测试.doc
play framework api,play! framework api,play api
为您提供Play Framework应用程序框架下载,Play Framework是一种高效的Java和Scala Web应用程序框架,该框架集成了用于现代Web应用程序开发的组件和API。Play由Web开发人员开发,用于Web应用程序开发。您会发现Play...