介绍 Play 框架最好的方法就是给一个完整的演示步骤让你看看 Play 到底有多简单。本演示使用最新的 Play 2.0 Beta 版。本文是在 Linux 环境下进行,如果你使用的 Windows,那会有一些小区别,例如路径和环境变量的设置等等,请自行解决。废话少说,下面我们开始:1. 下载并安装
1 | $ wget http://download.playframework.org/releases/play-2.0-beta.zip |
2 | $ unzip -q play-2.0-beta.zip |
3 | $ export PATH=$PATH:`pwd`/play-2.0-beta |
2. 创建一个新应用
03 | What is the application name? |
06 | Which template do you want to use for this new application? |
08 | 1 - Create a simple Scala application |
09 | 2 - Create a simple Java application |
10 | 3 - Create an empty project |
来看看都生成了什么文件?
03 | tasks/app/controllers/Application.java |
04 | tasks/app/views/index.scala.html |
05 | tasks/app/views/main.scala.html |
06 | tasks/conf/application.conf |
08 | tasks/project/build.properties |
09 | tasks/project/Build.scala |
10 | tasks/project/plugins.sbt |
11 | tasks/public/images/favicon.png |
12 | tasks/public/javascripts/jquery-1.6.4.min.js |
13 | tasks/public/stylesheets/main.css |
3. 运行程序
然后你就可以打开浏览器访问 http://localhost:9000 , 你看到什么了吗?接下来我们加点动态的数据编辑 app/views/index.scala.html 文件,内容如下:
编辑 app/controllers/Application.java 并修改 index() 方法,代码如下(我们故意少输入一个分号)
1 | return ok(index.render("Things")) |
刷新一下 http://localhost:9000 页面就会报一个模板编译错误:not found: value item.
该编译错误表明必须声明模板参数在控制台中,输入 Ctrl D 以停止 Play 程序,然后重新启动 Play 控制并编译程序:
在没有运行应用的情况下你也可以发现这个模板编译的错误再次启动应用:
在 app/views/index.scala.html 修复该错误刷新页面,将显示一个 Java 的编译错误:";" expected在 app/controllers/Application.java 中将那个缺少的分号加上。再次刷新页面,将显示 Things 标题在 public/stylesheets/main.css 中,我们添加一些 css 代码:
01 | body { font-family:"Helvetica Neue"; padding:2em; background: #B2EB5A url("/assets/images/play20header.png") no-repeat top center ; } |
02 | 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; } |
03 | ul { padding:0; list-style:none; } |
04 | 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; } |
05 | li a { text-decoration:none; color:transparent; position:absolute; top:1em; right:1em; } |
06 | li a:after { content:"?"; color:#aaa; font-size:120%; font-weight:bold; } |
07 | form * { font-size:120%; } |
09 | 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; } |
10 | p.error { margin:0; color:#c00; } |
在 app/controllers/Application.java 中,我们使用一个字符串 items 方法参数来替换 Things 字符串在 conf/routes 中,我们替换第一条路由信息(使用小写的 string)
1 | GET / controllers.Application.index(i: string) |
打开 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 中纠正这个错误:
1 | GET / controllers.Application.index(i: String) |
重新刷新页面撤销刚刚在 app/controllers/Application.java 中的更改,删除 index 方法参数:
1 | public static Result index(final String items) { |
2 | return ok(index.render(items)); |
撤销在 conf/routes 中的更改,删除参数:
1 | GET / controllers.Application.index() |
下面还有在 IDEA 环境中的使用以及表单处理和验证等,详情请看原文。