加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – List [String]的QueryStringBinder

发布时间:2020-12-16 18:03:31 所属栏目:安全 来源:网络整理
导读:使用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 binde
使用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.

我发现Play 2.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).asJava))
    def unbind(key: String,values: java.util.List[T]) = unbindList(key,values.asScala)
  }

  private def bindList[T: QueryStringBindable](key: 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")

解决方法

我终于解决了.使用的代码是:

在模型下,文件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,values)
  }

  private def bindList[T: QueryStringBindable](key: String,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快照的所有问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读