播放 2.4:表单:找不到参数消息的隐式值:play.api.i18n.Messages

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

我是 Play 框架的新手,并试图在我的本地机器上模仿 helloworld 示例,但我遇到了一个错误:

路线:

# Home page
GET        /                    controllers.Application.index

# Hello action
GET        /hello               controllers.Application.sayHello


# Map static resources from the /public folder to the /assets URL path
GET        /assets/*file        controllers.Assets.versioned(path="/public", file: Asset)

控制器:

package controllers

import play.api.mvc._
import play.api.data._
import play.api.data.Forms._

import views._

class Application extends Controller {

  val helloForm = Form(
    tuple(
      "name" -> nonEmptyText,
      "repeat" -> number(min = 1, max = 100),
      "color" -> optional(text)
    )
  )

  def index = Action {
    Ok(html.index(helloForm))
  }

  def sayHello = Action { implicit request =>
      helloForm.bindFromRequest.fold(
      formWithErrors => BadRequest(html.index(formWithErrors)),
      {case (name, repeat, color) => Ok(html.hello(name, repeat.toInt, color))}
    )
  }
}

查看:

@(helloForm: Form[(String,Int,Option[String])])

@import helper._

@main(title = "The 'helloworld' application") { 
    <h1>Configure your 'Hello world':</h1> 
    @form(action = routes.Application.sayHello, args = 'id -> "helloform") {
        @inputText(
            field = helloForm("name"),
            args = '_label -> "What's your name?", 'placeholder -> "World"
        )

        @inputText(
            field = helloForm("repeat"),
            args = '_label -> "How many times?", 'size -> 3, 'placeholder -> 10
        ) 
        @select(
            field = helloForm("color"),
            options = options(
                "" -> "Default",
                "red" -> "Red",
                "green" -> "Green",
                "blue" -> "Blue"
            ),
            args = '_label -> "Choose a color"
        ) 
        <p class="buttons">
            <input type="submit" id="submit">
        <p> 
    } 
}

我已经安装了 Play 2.4 并使用 IntelliJ Idea 14 通过 activator 模板创建了项目。

使用视图表单助手(例如 @inputText)需要您将隐式 play.api.i18n.Messages 参数传递给您的视图。您可以在您的视图中将 (implicit messages: Messages) 添加到签名中。你的观点变成了这样:

@(helloForm: Form[(String,Int,Option[String])])(implicit messages: Messages)

@import helper._

@main(title = "The 'helloworld' application") { 
  <h1>Configure your 'Hello world':</h1> 
  ...

然后在您的应用程序控制器中,您必须使该参数在您的范围内隐式可用。最简单的方法是实现 play 的 I18nSupport 特征。

在您的示例中,它看起来像这样:

package controllers

import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import javax.inject.Inject
import play.api.i18n.I18nSupport
import play.api.i18n.MessagesApi

import views._

class Application @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {

  val helloForm = Form(
    tuple(
      "name" -> nonEmptyText,
      "repeat" -> number(min = 1, max = 100),
      "color" -> optional(text)
    )
  )

  def index = Action {
    Ok(html.index(helloForm))
  }

  def sayHello = Action { implicit request =>
    helloForm.bindFromRequest.fold(
      formWithErrors => BadRequest(html.index(formWithErrors)),
      {case (name, repeat, color) => Ok(html.hello(name, repeat.toInt, color))}
    )
  }
}

在您的控制器中,您当然可以使用自己的 MessagesApi 实现。由于 play 知道如何注入 MessagesApi,您可以简单地用 @Inject 注释您的控制器,让 play 为您完成工作。

正如 Matthias Braun 提到的,您还必须设置

routesGenerator := InjectedRoutesGenerator

在你的 build.sbt

有关 I18n 的详细信息,请参阅 https://www.playframework.com/documentation/2.4.x/ScalaI18N

implicit messages 参数添加到视图后,您只需添加以下导入并使用旧控制器 类 甚至对象,无需任何其他更改:

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

使用表单助手需要您将隐式 play.api.i18n.Messages 参数传递给您的视图。您可以在您的视图中添加 (implicit messages: Messages) 来执行此操作。你的观点变成了这样:

@(contacts: List[models.Contact], 
  form: Form[models.Contact])(implicit messages: Messages)

然后手动注入你的控制器

import play.api.data.Forms._

import javax.inject.Inject

import play.api.i18n.I18nSupport

import play.api.i18n.MessagesApi 

然后最后添加到您的主索引控制器class

class Application @Inject()(val messagesApi: MessagesApi) extends
                                           Controller with I18nSupport {