4

Play 2.0.1を使用して、次のルートを定義しました。

GET  /demo/list controllers.Demos.listDemos(page: Int ?= 0, orderBy: Int ?= 1, nameFilter: String ?= "", versionFilter: Long ?= -1, tagFilter: List[String] ?= Nil)

しかし、コンパイル時にこのエラーが発生します。

No QueryString binder found for type List[String]. Try to implement an implicit QueryStringBindable for this type.

問題を解決するPlay2.1-RC(まだ正式にはリリースされていません)のコードを見つけました:

/**
* QueryString binder for List
*/
  implicit def bindableList[T: QueryStringBindable] = new QueryStringBindable[List[T]] {
    def bind(key: String, params: Map[String, Seq[String]]) = Some(Right(bindList[T](key, params)))
    def unbind(key: String, values: List[T]) = unbindList(key, values)
  }

  /**
* QueryString binder for java.util.List
*/
  implicit def bindableJavaList[T: QueryStringBindable] = new QueryStringBindable[java.util.List[T]] {
    def bind(key: String, params: Map[String, Seq[String]]) = Some(Right(bindList[T](key, params).asJava))
    def unbind(key: String, values: java.util.List[T]) = unbindList(key, values.asScala)
  }

  private def bindList[T: QueryStringBindable](key: String, params: Map[String, Seq[String]]): List[T] = {
    for {
      values <- params.get(key).toList
      rawValue <- values
      bound <- implicitly[QueryStringBindable[T]].bind(key, Map(key -> Seq(rawValue)))
      value <- bound.right.toOption
    } yield value
  }

  private def unbindList[T: QueryStringBindable](key: String, values: Iterable[T]): String = {
    (for (value <- values) yield {
      implicitly[QueryStringBindable[T]].unbind(key, value)
    }).mkString("&")
  }

しかし、数時間試しても、Playはまだコードを見つけられません。カスタムオブジェクトとそのオブジェクトへの暗黙の変換を使用しようとしましたが、それでも機能しません。

Playに暗黙の変換を強制するにはどうすればよいですか?

アップデート

2.1-SNAPSHOTは安定性が気になるので、なるべく避けたいのですが、他に方法がない場合は使用します。4e6のソリューションを試しましたが、機能しませんでした。

[warn]  ::::::::::::::::::::::::::::::::::::::::::::::                                 
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: org.slf4j#slf4j-api;1.6.1: configuration not found in org.slf4j#slf4j-api;1.6.1: 'compile'. It was required from org.hibernate#hibernate-validator;4.2.0.Final compile
[warn]  :: commons-codec#commons-codec;1.4: configuration not found in commons-codec#commons-codec;1.4: 'compile'. It was required from org.apache.httpcomponents#httpclient;4.1.2 compile
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::

私のsbtプラグインにはこれが含まれています:

// Comment to get more information during initialization
logLevel := Level.Warn

// The Typesafe repository
resolvers ++= Seq(
  Resolver.url("Typesafe Ivy Snapshots", url("http://repo.typesafe.com/typesafe/ivy-snapshots/"))(Resolver.ivyStylePatterns),
  "Typesafe Releases" at "http://repo.typesafe.com/typesafe/releases/",
  "Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/",
  "DefaultMavenRepository" at "http://repo1.maven.org/maven2/",
  "JavaNet1Repository" at "http://download.java.net/maven/1/")

// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.1-SNAPSHOT")
4

2 に答える 2

8

やっと解決しました。使用されるコードは次のとおりです。

モデルの下で、 QueryBinders.scalaをファイルします:

package models

import play.api.mvc.{JavascriptLitteral, QueryStringBindable}


/**
 * Created with IntelliJ IDEA.
 * User: pvillega
 * Date: 07/05/12
 * Time: 12:06
 * QueryStringBinders for some data types missing in 2.0.1
 */
//TODO: remove when updating to 2.1
object QueryBinders {

  /**
   * QueryString binder for List
   */
  implicit def bindableList[T: QueryStringBindable] = new QueryStringBindable[List[T]] {
    def bind(key: String, params: Map[String, Seq[String]]) = Some(Right(bindList[T](key, params)))
    def unbind(key: String, values: List[T]) = unbindList(key, values)
  }

  private def bindList[T: QueryStringBindable](key: String, params: Map[String, Seq[String]]): List[T] = {
    for {
      values <- params.get(key).toList
      rawValue <- values
      bound <- implicitly[QueryStringBindable[T]].bind(key, Map(key -> Seq(rawValue)))
      value <- bound.right.toOption
    } yield value
  }

  private def unbindList[T: QueryStringBindable](key: String, values: Iterable[T]): String = {
    (for (value <- values) yield {
      implicitly[QueryStringBindable[T]].unbind(key, value)
    }).mkString("&")
  }

  /**
   * Convert a Scala List[T] to Javascript array
   */
  implicit def litteralOption[T](implicit jsl: JavascriptLitteral[T]) = new JavascriptLitteral[List[T]] {
    def to(value: List[T]) = "[" + value.map { v => jsl.to(v)+"," } +"]"
  }

}

Build.scalaについて

 val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
      // Add your own project settings here
      lessEntryPoints <<= baseDirectory(_ / "app" / "assets" / "stylesheets" ** "style.less"),
      routesImport += "models.QueryBinders"
    )

これは2.1に更新するときに削除する必要がありますが、機能し、プロジェクトを2.1スナップショットに更新しようとしていたすべての問題を回避します。

于 2012-05-07T12:21:53.640 に答える
-1

2.1-SNAPSHOT代わりに、プロジェクトを移動して再生することができます(ドキュメントの「Playをインストールせずに新しいアプリケーションを作成する」セクションを参照してください)。必要なのは、sbt-pluginバージョンを変更してリゾルバーを追加することだけです。これが私のバージョンですplugins.sbt

resolvers ++= Seq(
  Resolver.url("Typesafe Ivy Snapshots", url("http://repo.typesafe.com/typesafe/ivy-snapshots/"))(Resolver.ivyStylePatterns),
  "Typesafe Releases" at "http://repo.typesafe.com/typesafe/releases/",
  "Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/",
  "Codahale Repo" at "http://repo.codahale.com")

addSbtPlugin("play" % "sbt-plugin" % "2.1-SNAPSHOT")

アップデート

slf4jなぜ依存関係に問題があるのか​​わかりません。タイプセーフリリースから正常にダウンロードされました。このリポジトリをアプリケーション構成に明示的に追加してみることができます(つまりBuild.scala、ではありませんplugins.sbt)。

[info] downloading http://repo.typesafe.com/typesafe/releases/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar ...
[info]  [SUCCESSFUL ] org.slf4j#slf4j-api;1.6.1!slf4j-api.jar (538ms)
于 2012-05-05T18:08:32.833 に答える