Play学习

在经过无数个尝试后,最终用sbt把play所依赖的所有包都下载下来了,现在可以非常快速编译运行了。今天体验了下网页模板,觉得非常不错,在这里做个简单的介绍。

原文说明:

复制代码
A Play Scala template is a simple text file that contains small blocks of Scala code. Templates can generate any text-based format, such as HTML, XML or CSV.

The template system has been designed to feel comfortable to those used to working with HTML, allowing front-end developers to easily work with the templates.

Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
复制代码

大致意思就是,你所建立的xxx.scala.html模板文件会编译成标准的scala对象和方法,比如说你创建一个index.scala.html,则会自动对应生成views.html.Application.index类,这里包含一个render的方法来指你本html。

我决定做一个路由,这个路由专门用来我学习play framework的,路径是/help,那么按照如下步骤:

1、编写模板文件 help.scala.html, 代码如下:

@(title: String)(message: String)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>@title</title>
</head>
<body>
    @play20.welcome(message, "java");
</body>
</html>

  @(title:String)(message:String)相当于这个模板的传入参数,可以把这个模板比作一个方法,最上面是定义模板的形式参数,下面的html语言是网页实现,通过@某一个参数可以获取参数的值,如代码中得@title。此时会自动生成views.html.Application.help类,包含一个方法render,这个方法的参数就是@(title:String)(message:String)。所以在action中就调用render来跳转到模板网页中去了。

如下代码是Play自动生成的help类,scala代码的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package  views.html
import  play.twirl.api. _
import  play.twirl.api.TemplateMagic. _
      object  help _ Scope 0  {
import  models. _
import  controllers. _
import  play.api.i 18 n. _
import  views.html. _
import  play.api.templates.PlayMagic. _
import  java.lang. _
import  java.util. _
import  scala.collection.JavaConversions. _
import  scala.collection.JavaConverters. _
import  play.core.j.PlayMagicForJava. _
import  play.mvc. _
import  play.data. _
import  play.api.data.Field
import  play.mvc.Http.Context.Implicit. _
class  help  extends  BaseScalaTemplate[play.twirl.api.HtmlFormat.Appendable,Format[play.twirl.api.HtmlFormat.Appendable]](play.twirl.api.HtmlFormat)  with  play.twirl.api.Template 2 [String,String,play.twirl.api.HtmlFormat.Appendable] {
   /**/
   def  apply /*1.2*/ (title :  String)(message :  String) : play.twirl.api.HtmlFormat.Appendable  =  {
     _ display _  {
       {
Seq[Any](format.raw /*1.34*/ ( "" "
" "" ),format.raw /*3.1*/ ( "" "<!DOCTYPE html>
<html lang=" en ">
<head>
   <meta charset=" UTF- 8 ">
   <title>" "" ), _ display _ ( /*7.11*/ title),format.raw /*7.16*/ ( "" "</title>
</head>
<body>
     " "" ), _ display _ ( /*10.6*/ play 20 /*10.12*/ .welcome(message,  "java" )),format.raw /*10.37*/ ( "" ";
</body>
</html>" "" ))
       }
     }
   }
   def  render(title : String,message : String) :  play.twirl.api.HtmlFormat.Appendable  =  apply(title)(message)
   def  f : ((String)  = > (String)  = > play.twirl.api.HtmlFormat.Appendable)  =  (title)  = > (message)  = > apply(title)(message)
   def  ref :  this . type  =  this
}
}
/**/
object  help  extends  help _ Scope 0 .help
               /*
                   -- GENERATED --
                   DATE: Sun Nov 29 14:50:01 CST 2015
                   SOURCE: /Users/cmlanche/web/play/workspace/play-java/app/views/help.scala.html
                   HASH: f50344195cfc33a1de3984c7d4cfc968c803534c
                   MATRIX: 750->1|877->33|905->35|1006->110|1031->115|1086->144|1101->150|1147->175
                   LINES: 27->1|32->1|34->3|38->7|38->7|41->10|41->10|41->10
                   -- GENERATED --
               */
@play20.welcome(message, "java"); 这个是play自带的说明文档网页

2、编写控制器,在Application中,或者说任意继承自Controller的类中

    public Result help() {
        return ok(help.render("Play Framework帮助文档", "this is doc of play framework for java"));
    }

3、映射路由

# Play Framework help doc
GET     /help                       controllers.Application.help()

这样就完工了,这时只需要打开 http://127.0.0.1:9000/help 就可以了。

开发完成后,只需要刷新页面即可,不需要重新run起来,这时Play的一大亮点,开发快速。

 

复制代码
Play!的关键特性:
1、一个非常简单的开发周期。此框架自动编译和重新装载源文件的任何改变。
2、智能捆绑HTTP参数到Java方法参数。
3、基于Jboss Netty框架的快速HTTP服务器,使Play能支持高并发、长连接以及静态文件优秀的输出能力。
4、一个基于Groovy的强大的模板引擎,具有多层继承,定制用户标签的能力,高可拓展性等。
5、优秀的错误报告功能:当发生异常,此框架会直接显示出错代码,甚至是模板代码。[1] 
6、易于实现非阻塞、大并发
复制代码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值