Play2.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. 创建一个新应用

 
 
 
  1. $ play new tasks
  2.  
  3. What is the application name?
  4. > tasks
  5.  
  6. Which template do you want to use for this new application?
  7.  
  8.   1 - Create a simple Scala application
  9.   2 - Create a simple Java application
  10.   3 - Create an empty project
  11.  
  12. > 2

来看看都生成了什么文件?

 
 
 
  1. $ find tasks -type f
  2. tasks/.gitignore
  3. tasks/app/controllers/Application.java
  4. tasks/app/views/index.scala.html
  5. tasks/app/views/main.scala.html
  6. tasks/conf/application.conf
  7. tasks/conf/routes
  8. tasks/project/build.properties
  9. 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. 运行程序

 
 
 
  1. $ cd tasks
  2. $ play run

然后你就可以打开浏览器访问 http://localhost:9000 , 你看到什么了吗?

接下来我们加点动态的数据

编辑 app/views/index.scala.html 文件,内容如下:

 
 
 
  1. @(items: String)
  2.  
  3. @main("Tasks") {
  4.     

    @item

  5. }

编辑 app/controllers/Application.java 并修改 index() 方法,代码如下(我们故意少输入一个分号)

 
 
 
  1. return 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 代码:

 
 
 
  1. body { font-family:"Helvetica Neue"; padding:2em; background: #B2EB5A url("/assets/images/play20header.png") no-repeat top center ; }
  2. 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; }
  3. ul { padding:0; list-style:none; }
  4. 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; }
  5. li a { text-decoration:none; color:transparent; position:absolute; top:1em; right:1em; }
  6. li a:after { content:''; color:#aaa; font-size:120%; font-weight:bold; }
  7. form * { font-size:120%; }
  8. input { width:16em; }
  9. 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));
  3. }

撤销在 conf/routes 中的更改,删除参数:

 
 
 
  1. GET /   controllers.Application.index()

下面还有在 IDEA 环境中的使用以及表单处理和验证等,详情请看原文。

原文连接:http://www.oschina.net/question/12_33439

当前题目:Play2.0的完整演示过程记录
链接分享:http://www.shufengxianlan.com/qtweb/news46/147246.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联