Scala下Play框架學(xué)習(xí)筆記(HttpRouting)-創(chuàng)新互聯(lián)

內(nèi)置的HTTP路由

創(chuàng)新互聯(lián)專注于博州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供博州營(yíng)銷型網(wǎng)站建設(shè),博州網(wǎng)站制作、博州網(wǎng)頁(yè)設(shè)計(jì)、博州網(wǎng)站官網(wǎng)定制、重慶小程序開(kāi)發(fā)服務(wù),打造博州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供博州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

路由把每一個(gè)引進(jìn)來(lái)的HTTP請(qǐng)求對(duì)應(yīng)到相應(yīng)的Action去。

HTTP請(qǐng)求被認(rèn)為是MVC框架下的一個(gè)事件,這一事件包含兩部分主要信息:

(1)請(qǐng)求路徑,包括query String

(2)HTTP方法,如get,post等

  路由被定義在經(jīng)過(guò)編譯的conf/routes中,因此你能在瀏覽器上直接看到報(bào)錯(cuò)信息。

conf/routes中每一行一般由HTTP方法和相應(yīng)URI組成:

GET  /clients/:id     controllers.Clients.show(id: Long)

也可加上評(píng)論:

# Display a client.

GET  /clients/:id     controllers.Clients.show(id: Long)

以下這個(gè)不太懂,請(qǐng)高人指教:

->   /api            api.MyRouter

HTTP方法

HTTP方法包括get、post、patch、put、delete、head等。

URI PATTERN

URI PATTERN定義了routes的路徑,部分請(qǐng)求路徑可以是動(dòng)態(tài)的。

(1)靜態(tài)路徑:

準(zhǔn)確匹配請(qǐng)求路徑,可以定義如下:

GET  /clients/all     controllers.Clients.list()

 (2)動(dòng)態(tài)的部分:

如果要通過(guò)ID來(lái)搜索路徑,需要用動(dòng)態(tài)指定的方式。

GET  /clients/:id     controllers.Clients.show(id: Long)

The default matching strategy for a dynamic part is defined by the regular expression [^/]+, meaning that any dynamic part defined as :id will match exactly one URI path segment.

通過(guò)*id可以來(lái)匹配多個(gè)URI,如下:

GET  /files/*name     controllers.Application.download(name)

以上的router,對(duì)于GET /files/p_w_picpaths/logo.png這樣的請(qǐng)求,name會(huì)匹配p_w_picpaths/logo.png。

用自定義正則表達(dá)式動(dòng)態(tài)路由

$id<regex>可以來(lái)自定義正則表達(dá)式

GET  /items/$id<[0-9]+>  controllers.Items.show(id: Long)

 the parameter is not decoded by the router or encoded by the reverse router. You’re responsible for validating the input to make sure it makes sense in that context.

controller action method

如果方法沒(méi)有參數(shù),可以如下定義:

GET  /           controllers.Application.homePage()

如果有參數(shù),則從請(qǐng)求URI中尋找參數(shù):

# Extract the page parameter from the path.

GET  /:page        controllers.Application.show(page)

或者如下:

# Extract the page parameter from the query string.

GET  /           controllers.Application.show(page)

show方法的定義如下:

def show(page: String) = Action {
 loadContentFromDatabase(page).map { htmlContent =>
  Ok(htmlContent).as("text/html")
 }.getOrElse(NotFound)

}

如果要把入?yún)⑥D(zhuǎn)換成scala的數(shù)據(jù)類型,就要有下面的router,并且實(shí)現(xiàn)相應(yīng)的方法:

GET  /clients/:id     controllers.Clients.show(id: Long)

def show(id: Long) = Action {
 Client.findById(id).map { client =>
  Ok(views.html.Clients.display(client))
 }.getOrElse(NotFound)

}

固定值的參數(shù):

# Extract the page parameter from the path, or fix the value for /
GET  /           controllers.Application.show(page = "home")

GET  /:page        controllers.Application.show(page)

默認(rèn)值方式:

# Pagination links, like /clients?page=3

GET  /clients       controllers.Clients.list(page: Int ?= 1)

Option類型,不需要在每一次傳參時(shí)都傳入:

# The version parameter is optional. E.g. /api/list-all?version=3.0

GET  /api/list-all     controllers.Api.list(version: Option[String])

注:多個(gè)routes沖突時(shí),使用第一個(gè)。

play.api.mvc.Call中提供了HTTP回調(diào),提供了HTTP和URI方法,它用如下方法創(chuàng)建控制器:

package controllers

import play.api._
import play.api.mvc._

class Application extends Controller {

 def hello(name: String) = Action {
  Ok("Hello " + name + "!")
 }

}

如果在conf/routes中有如下定義:

# Hello action
GET  /hello/:name     controllers.Application.hello(name)

那么可以通過(guò)controllers.routes.Application實(shí)現(xiàn)控制反轉(zhuǎn):

// Redirect to /hello/Bob
def helloBob = Action {
 Redirect(routes.Application.hello("Bob"))

}

默認(rèn)控制器:

# Redirects to https://www.playframework.com/ with 303 See Other
GET  /about   controllers.Default.redirect(to = "https://www.playframework.com/")

# Responds with 404 Not Found
GET  /orders   controllers.Default.notFound

# Responds with 500 Internal Server Error
GET  /clients  controllers.Default.error

# Responds with 501 Not Implemented
GET  /posts   controllers.Default.todo

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁(yè)標(biāo)題:Scala下Play框架學(xué)習(xí)筆記(HttpRouting)-創(chuàng)新互聯(lián)
文章地址:http://bm7419.com/article0/dscooo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)面包屑導(dǎo)航、網(wǎng)頁(yè)設(shè)計(jì)公司、電子商務(wù)軟件開(kāi)發(fā)、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)