播放 2.4:表单:找不到参数消息的隐式值:play.api.i18n.Messages [英] Play 2.4: Form: could not find implicit value for parameter messages: play.api.i18n.Messages

查看:21
本文介绍了播放 2.4:表单:找不到参数消息的隐式值:play.api.i18n.Messages的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Play 框架的新手,并试图在我的本地机器上模仿 helloworld 示例,但我遇到了一个错误:

I am new to Play framework and tried to mimic the helloworld sample in my local machine but I encountered an error:

路线:

# 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 并通过 activator 模板使用 IntelliJ Idea 14 创建了项目.

I have Play 2.4 installed and created the project using IntelliJ Idea 14 via activator template.

推荐答案

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

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:

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

这篇关于播放 2.4:表单:找不到参数消息的隐式值:play.api.i18n.Messages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆