Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms Play 2.4:Form:找不到参数消息的隐式值:Play.api.i18n.messages_Forms_Scala_Playframework_Playframework 2.4 - Fatal编程技术网

Forms Play 2.4:Form:找不到参数消息的隐式值:Play.api.i18n.messages

Forms Play 2.4:Form:找不到参数消息的隐式值:Play.api.i18n.messages,forms,scala,playframework,playframework-2.4,Forms,Scala,Playframework,Playframework 2.4,我不熟悉框架,尝试在本地机器中模拟helloworld示例,但遇到错误: 路线: # Home page GET / controllers.Application.index # Hello action GET /hello controllers.Application.sayHello # Map static resources from the /public folder to t

我不熟悉框架,尝试在本地机器中模拟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> 
    } 
}
控制器:

# 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> 
    } 
}
查看:

# 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> 
    } 
}
@(helloForm:Form[(String,Int,Option[String]))
@导入助手_
@main(title=“helloworld”应用程序){
配置您的“Hello world”:
@表单(action=routes.Application.sayHello,args='id->“helloform”){
@输入文本(
字段=helloForm(“名称”),
args=“\u标签->你叫什么名字?”,“占位符->世界”
)
@输入文本(
字段=helloForm(“重复”),
args='\u标签->多少次?,'大小->3',占位符->10
) 
@挑选(
字段=helloForm(“颜色”),
选项=选项(
“->”默认值“,
“红色”->“红色”,
“绿色”->“绿色”,
“蓝色”->“蓝色”
),
args='\u标签->“选择一种颜色”
) 

} }

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

使用视图表单帮助程序(如
@inputText
)需要将隐式
Play.api.i18n.Messages
参数传递给视图。您可以将
(隐式消息:消息)
添加到视图中的签名中。你的观点是:

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

@import helper._

@main(title = "The 'helloworld' application") { 
  <h1>Configure your 'Hello world':</h1> 
  ...
@(contacts: List[models.Contact], 
  form: Form[models.Contact])(implicit messages: Messages)
在控制器中,您当然可以使用自己的
MessagesApi
实现。由于play现成知道如何注入
MessagesApi
,您只需使用
@inject
为控制器添加注释,就可以让play为您完成这项工作

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

routesGenerator := InjectedRoutesGenerator
在您的
build.sbt


有关I18n的更多信息,请参阅。

在将
隐式消息
参数添加到视图后,您只需添加以下导入,并使用旧的控制器类,甚至对象,而无需进行任何其他更改:

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

使用表单帮助程序需要向视图传递隐式
play.api.i18n.Messages
参数。您可以将
(隐式消息:消息)
添加到视图中来执行此操作。你的观点是:

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

@import helper._

@main(title = "The 'helloworld' application") { 
  <h1>Configure your 'Hello world':</h1> 
  ...
@(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 Application @Inject()(val messagesApi: MessagesApi) extends
                                           Controller with I18nSupport {

我已经有:
类AppController@Inject()(val messages:MessagesApi)使用I18nSupport{…}扩展控制器
。如果我正确地理解了答案,那就足够了——但我仍然明白了这一点。@MichaelA。将
val messages:MessagesApi
更改为
val MessagesApi:MessagesApi
。这将自动覆盖
I18nSupport
.Doh中定义的
abstract def messagesApi
。现在可以工作了-非常感谢。一种你可以花上几个小时盯着看的错误…为了让它正常工作,我必须将
routesGenerator:=InjectedRoutesGenerator
添加到我的
build.sbt
中,并将路径前缀为
@
GET/hello@controllers.Application.sayHello
。我还必须添加
导入play.api.i18n.I18nSupport
导入play.api.i18n.MessagesApi
,才能将其添加到工作。为什么这不是文档推荐的方法?这似乎比扩展
I18Support
和注入
MessagesApi
要简单得多。我想Play框架开发人员会尝试推广更具定制性的新方法。导入
play.api.i18n.Messages.Implicits.\u
值会引入对play消息传递提供程序的硬编码依赖。依赖关系没有参数化,这在某些情况下不方便,例如,编写独立的单元测试。因此,使用具有参数化依赖关系的控制器类的方法更干净。然而,在我的应用程序中,使用具有硬编码依赖项的oldschool控制器对象通常就足够了。我更喜欢使用更简单的解决方案。可能值得一提的是,随着play的全局状态的删除,这种方法将被弃用。看,谢谢!这是转向新方法的一个很好的理由。这个答案主要是复制了以前对这个问题的部分答案!