Play !

conf目录主要存储一些配置信息

如你所见:conf目录下含有application.conf and routes两个文件

application.conf文件含有application的配置信息,其中配置日志级别、数据库连接池等信息


routes文件用于定义application的路由信息,把HTTP请求映射到具体方法实现上去

默认情况下含有两个记录:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index()

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)


以第一条记录为例:

对于'/'路径的GET请求,会映射到controllers.Application.index()方法上,play中称这种处理为 action methods


project目录~  其中的文件主要用于指定play!的构建配置

配置主要使用项目根目录下的build.sbt进行配置,project/build.properties 和project/plugins.sbt为SBT配置文件


build.sbt配置文件默认如下:

name := "application名称"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache
)     

play.Project.playJavaSettings


action method需要符合三个条件:public、static、返回类型为Result

说明下:TODO result表明method尚未实现


package controllers;

import play.mvc.Controller;
import play.mvc.Result;

/**
 * Created by 2011204 on 14-7-19.
 */
public class Products extends Controller {
    public  static Result list(){
        return TODO;
    }

    public static Result details(String name){
        return TODO;
    }

    public static  Result newProduct(){
        return TODO;
    }

    public static  Result save(){
        return TODO;
    }
}


routes添加路由信息:

GET  /products/       controllers.Products.list()
GET  /products/new    controllers.Products.newProduct()
GET  /products/:ean   controllers.Products.details(ean:String)
POST /products/       controllers.Products.save()


在浏览器打开 http://127.0.0.1:9000/products/ 会看到TODO Result  Action not implemented yet.


play template其中是由HTML和Scala两种语言组成,play会将scala代码编译成class文件用于渲染HTML。template位于application的view文件夹下,这样有助于和代码分离,看起来很清楚。


/app/views/main.scala.html这是由play所创建的模板,用于渲染HTML样板

public  static Result list(){
//  return TODO;
    List<Product>  products = Product.findAll();
    return ok(views.html.products.list.render(products));
}


Play会自动将模板文件编译成.class文件  也就是这个类中的render()方法

调用render方法的时候  也就是在调用


Form

Play将表单中的提交数据和一个Java对象相关联的过程成为data binding

Play中共有三种binding类型:

Form binding,URL query parameters binding和URL path binding

data  binding是一个简单的过程:HTTP参数值映射到Java对象中同名的属性。

每当参数名和Java对象属性名匹配的时候 会把HTTP参数的值赋给Java对象

Play中 from binding的 binding过程委托到spring data

URL query parameters binding过程


如果product有一个ID字段 对应的routeing

GET /products Products.edit(id: Long)

可以映射到如下的这个URL:

http://localhost:9000/products?id=12


URL path binding含有一个映射的路径,用于匹配Java对象中的属性


Play在数据绑定到模型的时候对数据进行校验

Play内建验证注解:

@Required 说明输入字段非空
@Min
说明输入最小数值
@Max 说明输入最大数值
@MinLength
说明输入字符串最小长度
@MaxLength 说明输入字符串最大长度
@Pattern 检查输入是否和正则表达式匹配
@ValidateWith 使用定制校验
@Email 检查是否是电子邮件格式



Play提供了@With注解  以此设计action

@With执行在action声明之前


异步数据处理:

One of the goals of handling data asynchronously is allowing our application to scale to thousands of concurrent connections while responding to clients immediately。



Play应用程序部署:

在生产环境下部署Play应用主要使用play stage和play dist两个命令

play stage命令将你的应用程序以及它所依赖的Jar编译成一个JAR文件   放置在target/stage下面  还有一个start脚本

可以直接运行target/stage脚本运行程序

play dist将你所需的所有文件打包成为一个zip文件  你可以将zip文件传送到指定Server上 解包之后运行start运行程序



你可能感兴趣的:(play)