如何在Play2中使用通配符定义url路由?

时间:2015-04-23 09:47:09

标签: scala playframework playframework-2.0 url-routing

我在Scala中使用 Play 2 。我想定义路由器规则,以便任何错误的URL重定向到根目录:

# Home page
GET     /                           controllers.Application.index(ignore="")

# global fall over
GET     /*ignore                    controllers.Application.index(ignore)

这很难看,我必须定义一个无用的参数来满足语法...任何想法如何删除ignore参数?

1 个答案:

答案 0 :(得分:6)

GlobalSettings目录中创建扩展app的对象:

import play.api.GlobalSettings
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent.Future

object Global extends GlobalSettings{
  override def onHandlerNotFound(request: RequestHeader) = {
    Future.successful(Redirect("/"))
  }
}

//routes
GET     /                           controllers.Application.index()

文档:https://www.playframework.com/documentation/2.3.x/ScalaGlobal