- 浏览: 2487344 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Playframework(11)Scala Project and First Example
Maybe, I will try emacs in the future, but this time, still I will use eclipse.
Project creation
>play new todolist
And this time I choose simple Scala application.
prepare the IDE
>cd todolist
>play eclipsify
Then we can import this project to eclipse.
Enter the play console and start the web container
>play
play>run
Visit http://localhost:9000 to make sure it is working.
Overview
Configuration is almost the same as Java. So there is no comments here.
Development workflow
Change the Application.scala to simple content.
object Application extends Controller{
def index = Action {
Ok("Hello Sillycat!")
}
}
Preparing the application
Did the same things as in Java Project, configure the routes file first.
And make the control TODO first.
def tasks = TODO
def newTask = TODO
def deleteTask(id: Long) = TODO
Prepare the Task model
Firstly create the package models under app.
The create a scala class Task.scala with Scala Wizards ---> Scala Object
The content are as follow, till now, the Scala language looks better than Java.
package models
case class Task(id: Long, label: String)
object Task {
def all() : List[Task] = Nil
def create(label: String) {}
def delete(id: Long) {}
}
The application template
Modify the index.scala.html template for task list and form just like in Java project.
The template engine is designed by Scala, it seems working with Scala backend is much better than with Java.
@(tasks: List[Task], taskForm: Form[String])
@import helper._
@main("Todo List") {
<h1]]>@tasks.size task(s)</h1]]>
<ul]]>
@tasks.map { task =>
<li]]>
@task.label
@form(routes.Application.deleteTask(task.id)){
<inputtype="submit"value="Delete"]]>
}
</li]]>
}
</ul]]>
<h2]]>Add a new task</h2]]>
@form(routes.Application.newTask){
@inputText(taskForm("label"))
<inputtype="submit"value="Create"]]>
}
}
The task form
A Form object encapsulates an HTML Form definition, including validation constraints.
import play.api.data._
import play.api.mvc._
import play.api.data.Forms._
val taskForm = Form(
"label" -> nonEmptyText
)
Rendering the first page
I implement the action tasks like this:
def tasks = Action {
Ok(views.html.index(Task.all(), taskForm))
}
but I got
Error Message:
too many arguments for method apply
Solution:
Right now, it seem right, but eclipse did not aware of that. I have no solution. I already have Scala plugins in my class.
Oops, I find the properties of the project and choose the Scala Compiler and uncheck the [Use Project Settings]
Handling the form submission
Handle and implement the newTask action
def newTask = Action { implicit request =>
taskForm.bindFromRequest.fold(
errors => BadRequest(views.html.index(Task.all(), errors)),
label => {
Task.create(label)
Redirect(routes.Application.tasks)
})
}
Persist the tasks in a database
Change the database configuration in conf/application.conf according to the Java Project.
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
The differences are that we need to create the SQL table for that.
create the file conf/evolutions/default/1.sql
#Tasks schema
# ----!Ups
CREATE SEQUENCE task_id_seq;
CREATETABLE task (
id integerNOTNULLDEFAULT nextval('task_id_seq'),
label varchar(255)
);
# --- !Downs
DROPTABLE task;
DROP SEQUENCE task_id_seq;
After I create the SQLs, I refresh the page. Play tell me we need evolution. I click on Apply this script. It is really magic.
Next step is to implement the SQL queries. Define the using of Anorm first
import anorm._
import anorm.SqlParser._
val task = {
get[Long]("id") ~
get[String]("label") map{
case id~label => Task(id, label)
}
}
import play.api.db._
import play.api.Play.current
def all() : List[Task] = DB.withConnection { implicit c =>
SQL("select * from task").as(task *)
}
Pay attention to the import statements, sometimes, eclipse can not import that for you.
def create(label: String) {
DB.withConnection{ implicit c =>
SQL("insert into task (label) values ( {label} ) ").on(
'label -> label
).executeUpdate()
}
}
def delete(id: Long) {
DB.withConnection { implicit c =>
SQL("delete from task where id = {id}").on(
'id -> id
).executeUpdate()
}
}
Adding the Deleting Tasks
def deleteTask(id: Long) = Action {
Task.delete(id)
Redirect(routes.Application.tasks())
}
It is there, it is done. Totally speaking, I feel play framework is really great, and it is worthing speed time on Scala. It is really a magic language.
Spring, Java and a lot of related J2EE framework, they are my old lovers now.
References:
http://www.playframework.org/documentation/2.0.4/ScalaTodoList
Maybe, I will try emacs in the future, but this time, still I will use eclipse.
Project creation
>play new todolist
And this time I choose simple Scala application.
prepare the IDE
>cd todolist
>play eclipsify
Then we can import this project to eclipse.
Enter the play console and start the web container
>play
play>run
Visit http://localhost:9000 to make sure it is working.
Overview
Configuration is almost the same as Java. So there is no comments here.
Development workflow
Change the Application.scala to simple content.
object Application extends Controller{
def index = Action {
Ok("Hello Sillycat!")
}
}
Preparing the application
Did the same things as in Java Project, configure the routes file first.
And make the control TODO first.
def tasks = TODO
def newTask = TODO
def deleteTask(id: Long) = TODO
Prepare the Task model
Firstly create the package models under app.
The create a scala class Task.scala with Scala Wizards ---> Scala Object
The content are as follow, till now, the Scala language looks better than Java.
package models
case class Task(id: Long, label: String)
object Task {
def all() : List[Task] = Nil
def create(label: String) {}
def delete(id: Long) {}
}
The application template
Modify the index.scala.html template for task list and form just like in Java project.
The template engine is designed by Scala, it seems working with Scala backend is much better than with Java.
@(tasks: List[Task], taskForm: Form[String])
@import helper._
@main("Todo List") {
<h1]]>@tasks.size task(s)</h1]]>
<ul]]>
@tasks.map { task =>
<li]]>
@task.label
@form(routes.Application.deleteTask(task.id)){
<inputtype="submit"value="Delete"]]>
}
</li]]>
}
</ul]]>
<h2]]>Add a new task</h2]]>
@form(routes.Application.newTask){
@inputText(taskForm("label"))
<inputtype="submit"value="Create"]]>
}
}
The task form
A Form object encapsulates an HTML Form definition, including validation constraints.
import play.api.data._
import play.api.mvc._
import play.api.data.Forms._
val taskForm = Form(
"label" -> nonEmptyText
)
Rendering the first page
I implement the action tasks like this:
def tasks = Action {
Ok(views.html.index(Task.all(), taskForm))
}
but I got
Error Message:
too many arguments for method apply
Solution:
Right now, it seem right, but eclipse did not aware of that. I have no solution. I already have Scala plugins in my class.
Oops, I find the properties of the project and choose the Scala Compiler and uncheck the [Use Project Settings]
Handling the form submission
Handle and implement the newTask action
def newTask = Action { implicit request =>
taskForm.bindFromRequest.fold(
errors => BadRequest(views.html.index(Task.all(), errors)),
label => {
Task.create(label)
Redirect(routes.Application.tasks)
})
}
Persist the tasks in a database
Change the database configuration in conf/application.conf according to the Java Project.
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
The differences are that we need to create the SQL table for that.
create the file conf/evolutions/default/1.sql
#Tasks schema
# ----!Ups
CREATE SEQUENCE task_id_seq;
CREATETABLE task (
id integerNOTNULLDEFAULT nextval('task_id_seq'),
label varchar(255)
);
# --- !Downs
DROPTABLE task;
DROP SEQUENCE task_id_seq;
After I create the SQLs, I refresh the page. Play tell me we need evolution. I click on Apply this script. It is really magic.
Next step is to implement the SQL queries. Define the using of Anorm first
import anorm._
import anorm.SqlParser._
val task = {
get[Long]("id") ~
get[String]("label") map{
case id~label => Task(id, label)
}
}
import play.api.db._
import play.api.Play.current
def all() : List[Task] = DB.withConnection { implicit c =>
SQL("select * from task").as(task *)
}
Pay attention to the import statements, sometimes, eclipse can not import that for you.
def create(label: String) {
DB.withConnection{ implicit c =>
SQL("insert into task (label) values ( {label} ) ").on(
'label -> label
).executeUpdate()
}
}
def delete(id: Long) {
DB.withConnection { implicit c =>
SQL("delete from task where id = {id}").on(
'id -> id
).executeUpdate()
}
}
Adding the Deleting Tasks
def deleteTask(id: Long) = Action {
Task.delete(id)
Redirect(routes.Application.tasks())
}
It is there, it is done. Totally speaking, I feel play framework is really great, and it is worthing speed time on Scala. It is really a magic language.
Spring, Java and a lot of related J2EE framework, they are my old lovers now.
References:
http://www.playframework.org/documentation/2.0.4/ScalaTodoList
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 433NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 294Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 377Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 337Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 419NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 363Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 293Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 209GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 392GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 277GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 265Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 262Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 253Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 238Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 235NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 227Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 518NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 231Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 321Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 322Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
Mastering Play Framework for Scala
Leverage the awesome features of Play Framework to build scalable, resilient, and responsive applications First published: May 2015 274page
mastering play framework for scala 英文原版数据和代码
Mastering Play Framework for Scala 英文无水印pdf pdf使用FoxitReader和PDF-XChangeViewer测试可以打开
Play Framework is an open source web application framework that is written in Java and Scala. It follows the Model-View-Controller architectural pattern and enables the user to employ Scala for ...
Mastering Play Framework for Scala 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Mastering Play Framework for Scala 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
This book is intended for those developers who are keen to master the internal workings of Play Framework to effectively build and deploy web-related apps.
PlayFramework框架安全模块.pdf
Play Framework是一種用Scala編寫的Web應用框架,其遵循模型-視圖-控制器建築模式。Play Framework使用Scala編寫,並可以被編譯成Java虛擬機器位元組碼中的其他編程語言使用;例如Java語言。
scala学习-project.zip
Play Framework DDD示例Play框架的DDD示例播放框架:2.6.x Scala(Scala):2.12.6用例文章:创建并显示文章详细信息帐户:创建帐户并登录draw.io上的设计域:/ designs 给我一颗星星 :glowing_star: 如果你喜欢他们...
Play Frmaework for Scala 完整电子书300页
PlayFramework 2.3 (Scala):使用子项目(包括另一个 Play 应用程序作为子项目) 如何在 PlayFramework 2.3 (Scala) 中创建子项目(模块)以及它们之间的依赖关系的示例。 需要注意的事项: 在 Root 项目中build.sbt...
Play for Scala shows you how to build Scala-based web applications using the Play 2 framework. This book starts by introducing Play through a comprehensive overview example. Then, you'll look at each ...
After that, the Scala web framework will be discussed in brief. Then, we will provide a comparative analysis of Java and Scala. Finally, we will dive into Scala programming to get started with Scala....
scala-js-example-app, 使用 Scala.js 构建的示例应用程序 用 Scala.js 编写的应用这是用 Scala.js 编写的应用程序的实例示例。开始要开始,在这个示例项目中打开 sbt,并执行任务 fastOptJS 。 这将创建文件 target...
The first part introduces you to Scala, helping you understand the object-oriented and functional programming concepts needed for Spark application development. It then moves on to Spark to cover the ...
A lot of example code to demonstrate Scala programming language, by the inventor of the language.