45

我是 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 创建了项目。

4

3 回答 3

65

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

import play.api.Play.current
import play.api.i18n.Messages.Implicits._
于 2015-06-15T11:11:18.353 回答
44

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

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

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 开箱即用地知道如何注入 a MessagesApi,因此您可以简单地注释控制器@Inject并让 play 为您完成工作。

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

routesGenerator := InjectedRoutesGenerator

在你的build.sbt

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

于 2015-06-12T10:28:42.067 回答
1

使用表单助手需要您将隐式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 Application @Inject()(val messagesApi: MessagesApi) extends
                                           Controller with I18nSupport {
于 2017-06-22T05:41:35.697 回答