【问题标题】:could not find implicit value for parameter messages: play.api.i18n.Messages in Play Framework 2.5.3找不到参数消息的隐式值:Play Framework 2.5.3 中的 play.api.i18n.Messages
【发布时间】:2016-09-19 04:41:08
【问题描述】:

我正在尝试使用 Play Framework 2.5.3 在本地测试示例路径变量。

 package controllers

 import play.api._
 import play.api.data._
 import play.api.data.Forms._
 import play.api.mvc._
 import play.api.libs.json._
 import play.api.libs.json.Json._
 import play.api.libs.functional.syntax._

 case class Product(sku: String, title: String)
 object Product {
implicit def pathBinder(implicit stringBinder: PathBindable[String]) = new PathBindable[Product] {
  override def bind(key: String, value: String): Either[String, Product]    = {
    for {
      sku <- stringBinder.bind(key, value).right
      product <- productMap.get(sku).toRight("Product not found").right
    } yield product
  }
  override def unbind(key: String, product: Product): String = {
    stringBinder.unbind(key, product.sku)
  }
 }

def add(product: Product) = productMap += (product.sku -> product)

val productMap = scala.collection.mutable.Map(
    "ABC" -> Product("ABC", "8-Port Switch"),
    "DEF" -> Product("DEF", "16-Port Switch"),
    "GHI" -> Product("GHI", "24-Port Switch")
)
}

object Products extends Controller {
  implicit val productWrites = new Writes[Product] {
  def writes(product: Product) = Json.obj(
    "sku" -> product.sku,
    "title" -> product.title
    )
  }

implicit val productReads: Reads[Product] = (
(JsPath \ "sku").read[String] and
(JsPath \ "title").read[String]
)(Product.apply _)

val productForm: Form[Product] = Form(
  mapping(
    "sku" -> nonEmptyText,
    "title" -> nonEmptyText
  )(Product.apply)(Product.unapply)
)

def index = Action {
    Ok(toJson(Product.productMap.values))
}

def postProduct = Action(BodyParsers.parse.json) { request =>
    val post = request.body.validate[Product]
  post.fold(
    errors => {
      BadRequest(Json.obj("status" ->"error", "message" -> JsError.toFlatJson(errors)))
    },
    product => { 
        Product.add(product)
      Created(toJson(product))
    }
  )
}

def edit(product: Product) = Action {
    Ok(views.html.products.form(product.sku, productForm.fill(product)))
}

def update(sku: String) = Action {
    Ok("Received update request")
}

}

以下是我的看法。

  @(sku: String, productForm: Form[controllers.Product])

  @helper.form(action = routes.Products.update(sku)) {
  @helper.inputText(productForm("sku"))
  @helper.inputText(productForm("title"))
  } 

当我编译这个程序时,我遇到了错误

 [error] /home/rajkumar/CodeBase/workspaceEclipse/PFC2/app/views/products/form.scala.html:4: could not find implicit value for parameter messages: play.api.i18n.Messages
  [error]   @helper.inputText(productForm("sku"))
   [error]                    ^
  [error] /home/rajkumar/CodeBase/workspaceEclipse/PFC2/app/views/products/form.scala.html:5: could not find implicit value for parameter messages: play.api.i18n.Messages
  [error]   @helper.inputText(productForm("title"))
   [error]                    ^
  [error] 9 errors found
  [error] (compile:compileIncremental) Compilation failed

谁能帮我解决这个问题。

【问题讨论】:

    标签: playframework playframework-2.0


    【解决方案1】:

    当您使用 inputText 时,需要“隐式消息”。通过导入以下语句添加它

    import play.api.Play.current
    import play.api.i18n.Messages.Implicits._
    

    在视图中你需要添加以下代码

    @(sku: String, productForm: Form[controllers.Product])(implicit messages: Messages)
    

    参考Play 2.4: Form: could not find implicit value for parameter messages: play.api.i18n.Messages

    【讨论】:

    • 我需要在控制器或views.html中添加这些包
    • @RajkumarNatarajan 不,你不需要将包添加到视图中,但你需要在视图中添加“隐式消息”,我已经更新了答案,祝你好运
    • 自 Play 2.5 起已弃用(特别是 play.api.Play.current),请参阅参考问题以获得正确答案。
    【解决方案2】:

    正如@jirka-helmich 指出的那样,@jerry 的解决方案已被弃用。

    Play 2.5.x documentation 列出了以下解决方案(注意 I18nSupport 特征):

    import javax.inject.Inject
    import play.api.i18n.I18nSupport
    class MyController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
    // ...
    

    更多详情请见this answer

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 2018-01-06
      • 2015-08-28
      • 2016-12-16
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多