Play框架之Hello, World!
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所提供的介面)
第一步:使用Play命令建立新的專案 這個時候系統會讓你在四個模板中間選擇一個 進入專案的主目錄 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
相關文章
- Flutter Web 之 Hello WorldFlutterWeb
- React 學習之 Hello WorldReact
- hello world .net core 微服務框架 Viper微服務框架
- Hello, World
- Hello World!
- Hello World
- Spring-Cloud之hello worldSpringCloud
- Go - Hello WorldGo
- Docker Hello WorldDocker
- 【Java】Hello worldJava
- React Hello,WorldReact
- Mockito Hello WorldMockito
- ant Hello World
- Deep "Hello world!"
- Go:Hello WorldGo
- Go Web 程式設計之 Hello WorldGoWeb程式設計
- R語言入門之Hello worldR語言
- Ruby語言入門之Hello world
- Hello Python worldPython
- react的”Hello World !“React
- WebGL 的 Hello WorldWeb
- ABAP程式Hello World
- dotnet hello world
- RabbitMQ tutorial - "Hello world!"MQ
- 輸出hello world
- C++併發程式設計框架Theron(4)——Hello world!C++程式設計框架
- Threes.js入門篇之2 - Hello WorldJS
- ROS之初見Hello WorldROS
- 【Flutter 基礎】Hello WorldFlutter
- JMicro微服務Hello World微服務
- 01-C++ "hello world"C++
- RabbitMQ 入門 - Hello WorldMQ
- [WebAssembly 入門] Hello, world!Web
- 機器學習,Hello World from Javascript!機器學習JavaScript
- Spring版Hello WorldSpring
- C# Hello,World(1)
- Viper 微服務框架 編寫一個hello world 外掛-02微服務框架
- 菜鳥學php擴充套件 之 hello world(一)PHP套件