Play框架之Hello, World!

gencat發表於2014-08-17

To make the page work, we'll define a combination of the following: 1. A view template (A template that generates HTML) 2. A controller action (A Scala function the renders the view) 3. Route configuration (Configuration to map the URL to the action) 4. The model (Scala code the defines the data structure and some test data)

大概的流程如圖所示(其實,控制器是最核心的,它使用了View Template和Model所提供的介面) enter image description here

第一步:使用Play命令建立新的專案 這個時候系統會讓你在四個模板中間選擇一個 enter image description here 進入專案的主目錄 cd products 執行專案 play run

第二步:Stylesheets 將bootstrap.css拷貝到public/stylesheets目錄中,這樣我們的template就可以連線到該檔案了。同時將glyphicons-halflings-whitepng和glyphicons-halflings.png拷貝至public/img目錄中。本專案也會用到自定義的css以覆蓋bootstrap的設定,這個css的名字即為main.css(參見Listing2.1)

第三步:新增Model A model class ( The definition of the product and its attributes) A data access object (DAO, code that provides access to product data) Test data (A set of product objects) 這些東西使用一個帶Object部分的case class就可以全部解決了

第四步:Product list pages 把這個檔案放在views.html.products包中。

@(products: List[Product]) (implicit lang:Lang)

@main(Message("application.name")){
    <dl class="products">
          @for(product <- products){
               <dt>@product.name</dt>
               <dd>@product.description</dd>
          }
     </dl>
}

This is a Scala template: an HTML document with embedded Scala statements, which start with an @character. 而該生成的HTML塊可以作為引數傳遞給另一個模組,即main。

第五步:Layout template

@(title: String)(content: Html)(implicit lang: Lang)
<!DOCTYPE html>
<html>
<head>
  <title>@title</title>
  <link rel="stylesheet" type="text/css" media="screen"
     href='@routes.Assets.at("stylesheets/bootstrap.css")'>
  <link rel="stylesheet" media="screen"
     href="@routes.Assets.at("stylesheets/main.css")">
</head>
<body>
<div class="screenshot">
  <div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
<a class="brand" href="@routes.Application.index()">
  @Messages("application.name")
</a> </div>
    </div>
  </div>
  <div class="container">
    @content
  </div>
</div>
</body>
</html>

The main purpose of this template is to provide a reusable structure for HTML pages in the application, with a common layout.

第六步:Controller action method

Model模組提供資料,Template模組提供格式化,而Controller協調二者。

package controllers
import play.api.mvc.{Action, Controller}
import models.Product
object Products extends Controller {
  def list = Action { implicit request =>
    val products = Product.findAll
    Ok(views.html.products.list(products))
  }
}

第七步:Adding a routes configuration The routes configuration specifies the mapping from HTTP to the Scala code in our controllers.we need to map the /products URL to the controllers.Products.list action. This means adding a new line in the conf/routes file.

GET /  controllers.Application.index
GET /products  controllers.Products.list
GET /assets/*file controllers.Assets.at(path="/public", file)

第八步:Replacing the welcome page with a redirect You can replace it with an HTTP redirect to the products list by changing the controller action in app/controllers/Application.scala to return an HTTP redirect response instead of rendering the default template.

package controllers
import play.api.mvc.{Action, Controller}
object Application extends Controller {
  def index = Action {
    Redirect(routes.Products.list())
} }

Detail Page 第一步:Adding a parameter to a controller action

相關文章