我將使用的方法是刪除請求,我有這個網址的web服務調用:如何從play2 mini中的路徑參數中提取值?
http://www.localhost:9000/example/com/foo/bar
我想提取/ foo和/條,並將其存儲在一個變量。
有沒有人知道如何做到這一點?謝謝!
我使用play2迷你Web服務和調度斯卡拉制作HTTP請求。
我將使用的方法是刪除請求,我有這個網址的web服務調用:如何從play2 mini中的路徑參數中提取值?
http://www.localhost:9000/example/com/foo/bar
我想提取/ foo和/條,並將其存儲在一個變量。
有沒有人知道如何做到這一點?謝謝!
我使用play2迷你Web服務和調度斯卡拉制作HTTP請求。
播放具有傳遞參數的一種非常複雜的方式,那就是通過路由的定義。在你的conf/routes文件定義路由像
GET /example/com/:foo controllers.yourcontroller.deleteAction(foo: String)
時,你會進入
http://www.localhost:9000/example/com/user
在瀏覽器
,deleteAction
你的控制器定義將被調用和字符串「用戶」將被傳遞給它在其參數foo
中。在那裏你可以操縱它。 請注意,部分URI後跟冒號是動態的。它將接受滿足作爲字符串要求的任何內容,並將它存儲在foo
變量中,並將其轉發給deleteAction。
您可以使用正則表達式作爲顯示在下面的代碼:
object App extends Application {
def route = Routes(
Through("/people/(.*)".r) {groups: List[String] =>
Action{
val id :: Nil = groups
Ok(<h1>It works with regex!, id: {id}</h1>).as("text/html")
}
},
{
case GET(Path("/coco")) & QueryString(qs) => Action{ request =>
println(request.body)
println(play.api.Play.current)
val result = QueryString(qs,"foo").getOrElse("noh")
Ok(<h1>It works!, query String {result}</h1>).as("text/html") }
case GET(Path("/cocoa")) => Action{ request =>
Ok(<h1>It works with extractors!</h1>).as("text/html") }
},
Through("/flowers/id/") {groups: List[String] =>
Action{
val id :: Nil = groups
Ok(<h1>It works with simple startsWith! - id: {id}</h1>).as("text/html")
}
}
)
}
是否使用play2迷你與Java或Scala呢? – iwalktheline 2012-07-14 14:44:30
@kheraud我使用scala。 – jeypijeypi 2012-07-15 10:24:39