1. 程式人生 > >play框架入門(1)

play框架入門(1)

2015-06-03

周海漢 2015.6.3

概述:

play框架是用scala寫的scala和java的框架,支援高併發,web和JPA。play框架得到一些網際網路新興企業的喜愛,相較spring,struts和hibernate/mybatis而言,更簡單高效。修改完程式碼無需重新打包部署,瀏覽器直接看到修改。

官網

下載,最新版2.4,是基於sbt的activator 進行編譯管理的:

老的版本2.2以前不需要activator

我們下載2.2.4.

  1. 生成新app,名字為 zhh

play new zhh

會讓你選擇專案名稱,所用語言。我們選java。

生成的檔案:

./.classpath

./.gitignore

./.project

./.settings

./.settings/org.eclipse.core.resources.prefs

./.settings/org.scala-ide.play2.prefs

./.settings/org.scala-ide.sdt.core.prefs

./app

./app/controllers

./app/controllers/Application.java

./app/views

./app/views/index.scala.html

./app/views/main.scala.html

./build.sbt

./conf

./conf/application.conf

./conf/routes

./logs

./project

./project/build.properties

./project/plugins.sbt

./project/project

2.執行,指定9001埠

[email protected] % play “run 9001”

  1. 在瀏覽器訪問

可以看到 Your new application is ready.

後面是play的介紹和原理解釋。

4.關鍵檔案

conf/routers配置路由:

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)

app/controllers/Application.java

package controllers;

import play.*;

import play.mvc.*;

import views.html.*;

public class Application extends Controller {

public static Result index() {

return ok(index.render(“Your new application is ready.世界,你好!”));

}

}

模板放在views裡面。 模板的語言是scala,scala相當於簡化和方便化的java語言,用於模板很強大,可以實現模板繼承等關係。

app/views/index.scala.html

@(message: String)

@main(“Welcome to Play”) {

@play20.welcome(message, style = “Java”)

app/views/main.scala.html

@(title: String)(content: Html)

<!DOCTYPE html>

@title <link rel="stylesheet" media="screen" href="@routes.Assets.at("_stylesheets__/__main.css__")"_> <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("_images__/__favicon.png__")"_> <script src="@routes.Assets.at("_javascripts__/__jquery-1.9.0.min.js__")"_ type="text/javascript"></script> @content

}

5.原理

使用者通過瀏覽器訪問根/時,play框架會根據routers裡面的配置,找到controllers.Application.index(),這會呼叫Application類的index()函式,

index.render(“Your new application is ready.世界,你好!”)

會將“Your new application is ready.世界,你好!”傳到index.scala.html. index.scala.html再呼叫main.scala.html, 將訊息和@play20的內容傳遞到main模板。

我們修改一下程式和模板,進行測試。

首先,將index.scala.html修改為index1.scala.html

重新整理瀏覽器,報錯:

compile error 錯誤: 找不到符號

將Application index函式內容改為:

return ok(index1.render(“Your new application is ready.世界,你好!”);

則沒有報錯。

我們再給模板傳遞多一個引數,修改Application.java:

return ok(index1.render(“Your new application is ready.世界,你好!”,”zhh 測試內容”));

index1.scala.html修改為:

@(message1: String,message2:String)

@main(“Welcome to Play”,message2) {

@play20.welcome(message1, style = “Java”)

main.scala.html修改為:

@(title: String, msg:String)(content: Html)

<!DOCTYPE html>

@title <link rel="stylesheet" media="screen" href="@routes.Assets.at("_stylesheets__/__main.css__")"_> <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("_images__/__favicon.png__")"_> <script src="@routes.Assets.at("_javascripts__/__jquery-1.9.0.min.js__")"_ type="text/javascript"></script>

[email protected]_

@content

重新整理後,看到我從程式傳遞到web模板的內容。

}

如非註明轉載, 均為原創. 本站遵循知識共享CC協議,轉載請註明來源