简体   繁体   English

Play 2.4:Form:找不到参数消息的隐含值:play.api.i18n.Messages

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

I am new to Play framework and tried to mimic the helloworld sample in my local machine but I encountered an error: 我是Play框架的新手并尝试在我的本地机器中模仿helloworld示例,但是我遇到了一个错误:

在此输入图像描述

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 activator template. 我安装了Play 2.4并使用IntelliJ Idea 14通过激活器模板创建了项目。

After adding implicit messages parameters 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._

Using view form helpers (such as @inputText ) requires you to pass an implicit play.api.i18n.Messages parameter to your view. 使用视图表单助手(例如@inputText )要求您将隐式的play.api.i18n.Messages参数传递给视图。 You can do this adding (implicit messages: Messages) to the signature in your view. 您可以执行此操作(implicit messages: Messages)到视图中的签名。 Your view becomes this: 你的观点变成了这样:

@(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 I18nSupport trait. 最简单的方法是实现play的I18nSupport特性。

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 . 在您的控制器中,您当然可以使用自己的MessagesApi实现。 Since play knows out of the box how to inject a MessagesApi you can simply annotate your controller with @Inject and let play do the work for you. 由于play知道开箱即用如何注入MessagesApi你可以使用@Inject简单地注释你的控制器,然后让play为你完成工作。

As Matthias Braun mentioned, you also have to set 正如Matthias Braun所说,你也必须设定

routesGenerator := InjectedRoutesGenerator

in your build.sbt 在你的build.sbt

See https://www.playframework.com/documentation/2.4.x/ScalaI18N for more information about I18n. 有关I18n的更多信息,请参见https://www.playframework.com/documentation/2.4.x/ScalaI18N

Using form helpers requires you to pass an implicit play.api.i18n.Messages parameter to your view. 使用表单助手需要您将隐式的play.api.i18n.Messages参数传递给视图。 You can do this adding (implicit messages: Messages) to in your view. 您可以在视图中添加(implicit messages: Messages) Your view becomes this: 你的观点变成了这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM