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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30799988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:15:22  来源:igfitidea点击:

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

formsscalaplayframeworkplayframework-2.4

提问by arjayads

I am new to Play framework and tried to mimic the helloworldsample in my local machine but I encountered an error:

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

enter image description here

在此处输入图片说明

routes:

路线:

# 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)

controller:

控制器:

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))}
    )
  }
}

view:

看法:

@(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> 
    } 
}

I have Play 2.4 installed and created the project using IntelliJ Idea 14 via activatortemplate.

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

回答by ps_ttf

After adding implicit messagesparameters to views you can just add the following imports and use the old controller classes or even objects without any additional changes:

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

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

回答by Roman

Using view form helpers (such as @inputText) requires you to pass an implicit play.api.i18n.Messagesparameter to your view. You can do this adding (implicit messages: Messages)to the signature in your view. Your view becomes this:

使用视图表单助手(例如@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> 
  ...

Then in your application controller you must make this parameter implicitly available in your scope. The simplest way to do this is to implement play's I18nSupporttrait.

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

In your example, this would look like this:

在您的示例中,这将如下所示:

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))}
    )
  }
}

In your controller you can of course use your own implementation of MessagesApi. Since play knows out of the box how to inject a MessagesApiyou can simply annotate your controller with @Injectand let play do the work for you.

在您的控制器中,您当然可以使用自己的MessagesApi. 由于 play 开箱即用地知道如何注入 a,因此MessagesApi您可以简单地用它来注释您的控制器,@Inject让 play 为您完成工作。

As Matthias Braun mentioned, you also have to set

正如马蒂亚斯布劳恩所提到的,你还必须设置

routesGenerator := InjectedRoutesGenerator

in your build.sbt

在你的 build.sbt

See https://www.playframework.com/documentation/2.4.x/ScalaI18Nfor more information about I18n.

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

回答by arvind grey

Using form helpers requires you to pass an implicit play.api.i18n.Messagesparameter to your view. You can do this adding (implicit messages: Messages)to in your view. Your view becomes this:

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

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

Then manually inject into your controllers

然后手动注入您的控制器

import play.api.data.Forms._

import javax.inject.Inject

import play.api.i18n.I18nSupport

import play.api.i18n.MessagesApi 

then finally add on to your main index controller class

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

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