Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Play 2.0 的完整演示过程记录

介绍 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. 创建一个新应用 
01$ play new tasks
02   
03What is the application name?
04> tasks
05   
06Which template do you want to use for this new application?
07   
08  1 - Create a simple Scala application
09  2 - Create a simple Java application
10  3 - Create an empty project
11   
12> 2
来看看都生成了什么文件? 
01$ find tasks -type f
02tasks/.gitignore
03tasks/app/controllers/Application.java
04tasks/app/views/index.scala.html
05tasks/app/views/main.scala.html
06tasks/conf/application.conf
07tasks/conf/routes
08tasks/project/build.properties
09tasks/project/Build.scala
10tasks/project/plugins.sbt
11tasks/public/images/favicon.png
12tasks/public/javascripts/jquery-1.6.4.min.js
13tasks/public/stylesheets/main.css
3. 运行程序 
1$ cd tasks
2$ play run
然后你就可以打开浏览器访问 http://localhost:9000 , 你看到什么了吗?接下来我们加点动态的数据编辑 app/views/index.scala.html 文件,内容如下: 
1@(items: String)
2   
3@main("Tasks") {
4    <h1>@item</h1>
5}
编辑 app/controllers/Application.java 并修改 index() 方法,代码如下(我们故意少输入一个分号) 
1return ok(index.render("Things"))
刷新一下 http://localhost:9000 页面就会报一个模板编译错误:not found: value item.该编译错误表明必须声明模板参数在控制台中,输入 Ctrl D 以停止 Play 程序,然后重新启动 Play 控制并编译程序: 
1$ play
2[tasks] $ compile
在没有运行应用的情况下你也可以发现这个模板编译的错误再次启动应用: 
1[tasks] $ run
在 app/views/index.scala.html 修复该错误刷新页面,将显示一个 Java 的编译错误:";" expected在 app/controllers/Application.java 中将那个缺少的分号加上。再次刷新页面,将显示 Things 标题在 public/stylesheets/main.css 中,我们添加一些 css 代码: 
01body { font-family:"Helvetica Neue"; padding:2em; background: #B2EB5A url"/assets/images/play20header.png"no-repeat top center ; }
02body: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; }
03ul { padding:0; list-style:none; }
04li, form { width:30em; background:white; padding:1em; border:1px solid #ccc; border-radius:0.5em; margin:1em 0; position:relative; min-height:1.2em; }
05li a { text-decoration:none; color:transparent; position:absolute; top:1em; right:1em; }
06li a:after { content:"?"; color:#aaa; font-size:120%; font-weight:bold; }
07form * { font-size:120%; }
08input { width:16em; }
09button { 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; }
10p.error { margin:0; color:#c00; }
在 app/controllers/Application.java 中,我们使用一个字符串 items 方法参数来替换 Things 字符串在 conf/routes 中,我们替换第一条路由信息(使用小写的 string) 
1GET /   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 中纠正这个错误: 
1GET /   controllers.Application.index(i: String)
重新刷新页面撤销刚刚在 app/controllers/Application.java 中的更改,删除 index 方法参数: 
1public static Result index(final String items) {
2   return ok(index.render(items));
3}
撤销在 conf/routes 中的更改,删除参数: 
1GET /   controllers.Application.index()
下面还有在 IDEA 环境中的使用以及表单处理和验证等,详情请看原文。