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

时间:2015-06-12 09:42:18

标签: forms scala playframework playframework-2.4

我是Play框架的新手并试图模仿我本地计算机中的 helloworld 示例,但我遇到了错误:

enter image description here

路线:

# 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通过激活器模板创建了项目。

3 个答案:

答案 0 :(得分:65)

"packages cannot be nested"参数添加到视图后,您只需添加以下导入并使用旧的控制器类甚至对象,而无需任何其他更改:

package actions {    

    import flash.display.MovieClip;
    import flash.events.*;  

    public class main extends MovieClip {

        public function main ():void {             
            submit_button.buttonMode = true;
            submit_button.addEventListener(MouseEvent.MOUSE_DOWN, checkLogin);

            username.text = "";
            password.text = "";         
        }      

        public function checkLogin (e:MouseEvent):void {         
            trace("submission success");         
        }     
    } 
}

答案 1 :(得分:43)

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

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

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开箱即可知道如何注入MessagesApi,您只需使用@Inject注释您的控制器,然后让游戏为您完成工作。

Matthias Braun提到,你还必须设置

routesGenerator := InjectedRoutesGenerator

build.sbt

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

答案 2 :(得分: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 {