【问题标题】:play scala routes configuration播放 scala 路由配置
【发布时间】:2016-09-11 21:46:21
【问题描述】:

我是 Play scala 框架的新手。我正在尝试根据输入的 url 返回虚拟数据。请看以下场景:

GET /test    ---> return fist test passed
GET /test/10   ---> return with param test passed
GET /test?id=10   ---> return with query param test passed

 other wise ---> return no test passed

在路线中,我定义如下:

GET  /test/:key     com.test.controller.TestController.index(key: Option[String])

控制器:

def index[A](key: Option[String])  = Action {implicit request =>
    val test= key match {
      case Some(type :String)=> 
        Ok("first test")

      case None =>
             Ok("No test found")
    }
  }

在控制器中,我不确定如何检查所有场景。请在这方面帮助我

【问题讨论】:

  • 正如你所标记的,你真的开始 playframework2.0 了吗?它太旧了,无法用于新项目。如果您使用 2.1 或更新版本,答案会有所不同。
  • 你能分享任何做同样事情的例子吗?

标签: scala routes playframework-2.0


【解决方案1】:

使用 Play 2.0 更高版本..

路由文件

GET  /test/:key     com.test.controller.TestController.index(key: Option[String])

控制器:

def index(key: Option[String])  = Action { implicit request =>
  key match {
    case Some(type :String)=> Ok("first test")
    case _ => Ok("No test found")
  }
}

也许这可以解决您的问题..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2018-04-08
    相关资源
    最近更新 更多