介绍 Play 框架最好的方法就是给一个完整的演示步骤让你看看 Play 到底有多简单。本演示使用最新的 Play 2.0 Beta 版。
本文是在 Linux 环境下进行,如果你使用的 Windows,那会有一些小区别,例如路径和环境变量的设置等等,请自行解决。
废话少说,下面我们开始:
1. 下载并安装
复制
$ wget http://download.playframework.org/releases/play-2.0-beta.zip $ unzip -q play-2.0-beta.zip $ export PATH=$PATH:`pwd`/play-2.0-beta
1.
2.
3.
2. 创建一个新应用
复制
$ play new tasks What is the application name? > tasks Which template do you want to use for this new application? 1 - Create a simple Scala application 2 - Create a simple Java application 3 - Create an empty project > 2
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
来看看都生成了什么文件?
复制
$ find tasks -type f tasks/.gitignore tasks/app/controllers/Application.java tasks/app/views/index.scala.html tasks/app/views/main.scala.html tasks/conf/application.conf tasks/conf/routes tasks/project/build.properties tasks/project/Build.scala tasks/project/plugins.sbt tasks/public/images/favicon.png tasks/public/javascripts/jquery-1.6.4.min.js tasks/public/stylesheets/main.css
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
3. 运行程序
复制
$ cd tasks $ play run
1.
2.
然后你就可以打开浏览器访问 http://localhost:9000 , 你看到什么了吗?
接下来我们加点动态的数据
编辑 app/views/index.scala.html 文件,内容如下:
复制
@(items: String) @main("Tasks") { <h1>@item</h1> }
1.
2.
3.
4.
5.
编辑 app/controllers/Application.java 并修改 index() 方法,代码如下(我们故意少输入一个分号)
复制
return ok(index.render("Things"))
1.
刷新一下 http://localhost:9000 页面就会报一个模板编译错误:not found: value item.
该编译错误表明必须声明模板参数
在控制台中,输入 Ctrl D 以停止 Play 程序,然后重新启动 Play 控制并编译程序:
复制
$ play [tasks] $ compile
1.
2.
在没有运行应用的情况下你也可以发现这个模板编译的错误
再次启动应用:
复制
[tasks] $ run
1.
在 app/views/index.scala.html 修复该错误
刷新页面,将显示一个 Java 的编译错误:';' expected
在 app/controllers/Application.java 中将那个缺少的分号加上。
再次刷新页面,将显示 Things 标题
在 public/stylesheets/main.css 中,我们添加一些 css 代码:
复制
body { font-family:"Helvetica Neue"; padding:2em; background: #B2EB5A url("/assets/images/play20header.png") no-repeat top center ; } body:before { content:'Play 2.0 task list demo'; color:rgba(255,255,255,0.7); font-size:150%; text-transform:uppercase; letter-spacing:0.4em; } ul { padding:0; list-style:none; } li, form { width:30em; background:white; padding:1em; border:1px solid #ccc; border-radius:0.5em; margin:1em 0; position:relative; min-height:1.2em; } li a { text-decoration:none; color:transparent; position:absolute; top:1em; right:1em; } li a:after { content:'❎'; color:#aaa; font-size:120%; font-weight:bold; } form * { font-size:120%; } input { width:16em; } button { cursor:pointer; color: white; background-color: #3D6F04; background-image: -webkit-linear-gradient(top, #5AA706, #3D6F04); text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); border: 1px solid #CCC; border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25); border-radius:4px; } p.error { margin:0; color:#c00; }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
在 app/controllers/Application.java 中,我们使用一个字符串 items 方法参数来替换 Things 字符串
在 conf/routes 中,我们替换第一条路由信息(使用小写的 string)
复制
GET / controllers.Application.index(i: string)
1.
打开 http://localhost:9000/?items=Tasks 将显示路由编译错误信息:
The routes file is compiled, and HTTP parameters must be declared. HTTP parameter names do not have to match the action method names.
在 conf/routes 中纠正这个错误:
复制
GET / controllers.Application.index(i: String)
1.
重新刷新页面
撤销刚刚在 app/controllers/Application.java 中的更改,删除 index 方法参数:
复制
public static Result index(final String items) { return ok(index.render(items)); }
1.
2.
3.
撤销在 conf/routes 中的更改,删除参数:
复制
GET / controllers.Application.index()
1.
下面还有在 IDEA 环境中的使用以及表单处理和验证等,详情请看原文。
原文连接:http://www.oschina.net/question/12_33439
【编辑推荐】