使用scala在Play框架中找不到Implicit Messages Provider实例

ami*_*mit 3 scala playframework

我正在使用与scala的playframework,我正在尝试构建表单,但得到以下错误

"找不到隐式的MessagesProvider实例.请参阅https://www.playframework.com/documentation/2.6.x/ScalaForms#Passing-MessagesProvider-to-Form-Helpers "这里是我的index.scala.html

@(customerForm:Form[Customer])

@import helper._

@main("welcome") {
    <h1>Customer Form</h1>
    @form(action=routes.Application.createCustomer()) {
        @inputText(customerForm("Credit Limit"))
        @inputText(customerForm("Customer Name"))
        <input type="submit" value="Submit">
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序控制器代码

package controllers

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

class Application extends Controller {

  def customerForm = Form(mapping("Customer Name" -> nonEmptyText,
    "Credit Limit" -> number)(Customer.apply)(Customer.unapply))

  def index = Action { implicit request =>
    Ok(views.html.index(customerForm))
  }

  def createCustomer = Action { implicit request =>
    customerForm.bindFromRequest().fold(
      formWithErrors => BadRequest(views.html.index(formWithErrors)),
      customer => Ok(s"Customer ${customer.name} created successfully"))
  }

}
Run Code Online (Sandbox Code Playgroud)

Ach*_*jr. 10

游戏框架表单处理在版本2.5和2.6之间已经改变,为了使事情工作,您必须更改Application类的声明,如下所示:

import javax.inject._
import play.api.i18n.I18nSupport

class Application @Inject()(val cc: ControllerComponents) extends AbstractController(cc) with I18nSupport
Run Code Online (Sandbox Code Playgroud)

并在您的视图中添加一个隐式参数,如下所示:

@(customerForm:Form[Customer])(implicit request: RequestHeader, messagesProvider: MessagesProvider)
Run Code Online (Sandbox Code Playgroud)

如果您在视图中不需要RequestHeader,则可以省略其声明.

有关详细信息,请参阅错误消息中的链接:https: //www.playframework.com/documentation/2.6.x/ScalaForms#Passing-MessagesProvider-to-Form-Helpers