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
- Spring-Cloud之hello worldSpringCloud
- hello world .net core 微服務框架 Viper微服務框架
- Hello, World
- Hello,World
- Hello World
- Hello World!
- Hello World !
- Go - Hello WorldGo
- Deep "Hello world!"
- Hello Python worldPython
- Hello World探究
- Docker Hello WorldDocker
- dotnet hello world
- Go:Hello WorldGo
- ant Hello World
- Go Web 程式設計之 Hello WorldGoWeb程式設計
- I'm Hello World
- 輸出hello world
- RabbitMQ tutorial - "Hello world!"MQ
- WebGL 的 Hello WorldWeb
- react的”Hello World !“React
- Threes.js入門篇之2 - Hello WorldJS
- [系列] Go gRPC Hello WorldGoRPC
- Hello World! XJ is here.
- 01-C++ "hello world"C++
- python輸出hello worldPython
- C# Hello,World(1)
- [WebAssembly 入門] Hello, world!Web
- RabbitMQ 入門 - Hello WorldMQ
- spring boot(一)hello worldSpring Boot
- 【Flutter 基礎】Hello WorldFlutter
- JMicro微服務Hello World微服務
- ROS之初見Hello WorldROS
- Viper 微服務框架 編寫一個hello world 外掛-02微服務框架
- PHPCPP安裝以及hello worldPHP
- Smali 語法解析——Hello World
- spring boot(一)hello world 搭建Spring Boot