在Play 2.4.3的范围内具有自定义QueryStringBindable

mpa*_*tan 4 scala query-string querystringparameter playframework playframework-2.4

我想在我的Play-scala项目中使用java.sql.Date和Option [java.sql.Date]作为查询参数,但Play框架没有默认设置。我正在使用的播放版本是2.4.3。我有以下(粗略的)课程。

object CustomBinders extends {
  val dateFormat = ISODateTimeFormat.date()

  implicit def dateBinder: QueryStringBindable[Date] = new QueryStringBindable[Date] {
    def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Date]] = {
      val dateString: Option[Seq[String]] = params.get(key)
      try {
        Some(Right(new Date(dateFormat.parseDateTime(dateString.get.head).getMillis)))
      } catch {
        case e: IllegalArgumentException => Option(Left(dateString.get.head))
      }
    }

    def unbind(key: String, value: Date): String = {
      dateFormat.print(value.getTime)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在Build.scala中,我有

import play.sbt.routes.RoutesKeys

object Build extends Build {
  RoutesKeys.routesImport += "binders.CustomBinders.dateBinder"
  RoutesKeys.routesImport += "binders.CustomBinders.optionDateBinder"
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用Option [Date]定义查询参数,则会出现错误

No QueryString binder found for type Option[java.sql.Date]. Try to implement an implicit QueryStringBindable for this type.
Run Code Online (Sandbox Code Playgroud)

因此,显然这不是范围。我应该如何定义绑定器,使它们存在于示波器中?我找不到2.4文档,但是2.5文档没有说明需要将它们添加到Build.scala中的任何内容

mpa*_*tan 5

因此,很显然Build.scala并不是正确的地方...即使一些文档告诉您将它放在那里。在build.sbt中

routesImport += "binders.CustomBinders._"
Run Code Online (Sandbox Code Playgroud)

该项目编译很好。修复了活页夹原始帖子中的一些错误。