복붙노트

[SCALA] 2.4 재생 : 양식 : 매개 변수 메시지에 대한 암시 적 가치를 찾을 수 없습니다 : play.api.i18n.Messages를

SCALA

2.4 재생 : 양식 : 매개 변수 메시지에 대한 암시 적 가치를 찾을 수 없습니다 : play.api.i18n.Messages를

나는 내 로컬 컴퓨터에서 프레임 워크와 모방 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> 
    } 
}

나는 설치된 2.4 재생이 있고 활성 템플릿을 통해 인 IntelliJ 아이디어 (14)를 사용하여 프로젝트를 만들었습니다.

해결법

  1. ==============================

    1.뷰에 암시 적 메시지의 매개 변수를 추가 한 후에는 다음과 같은 수입을 추가하고 기존 컨트롤러 클래스를 사용하거나 추가 변경없이 개체 수 있습니다 :

    뷰에 암시 적 메시지의 매개 변수를 추가 한 후에는 다음과 같은 수입을 추가하고 기존 컨트롤러 클래스를 사용하거나 추가 변경없이 개체 수 있습니다 :

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

    2.(예 : @inputText 등)보기 양식 헬퍼를 사용하면보기에 암시 적 play.api.i18n.Messages 매개 변수를 전달할 수 있어야합니다. 보기에 서명 :이 추가 (메시지 암시 적 메시지를) 할 수 있습니다. 보기이된다 :

    (예 : @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> 
      ...
    

    그런 다음 응용 프로그램 컨트롤러에 당신은 당신의 범위이 매개 변수가 암시 적으로 사용할 수 있도록해야합니다. 이 작업을 수행하는 가장 간단한 방법은 놀이의 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 당신 자신의 구현을 사용할 수 있습니다. 플레이가 상자 밖으로 알고 있기 때문에 어떻게 당신은 단순히 @Inject와 컨트롤러에 주석을 달 수있는 MessagesApi를 주입 놀이 원하는 작업을 할 수 있습니다.

    마티아스 브라운은 언급 한 바와 같이, 당신은 또한 설정해야

    routesGenerator := InjectedRoutesGenerator
    

    당신의 build.sbt에

    국제화에 대한 자세한 내용은 https://www.playframework.com/documentation/2.4.x/ScalaI18N를 참조하십시오.

  3. ==============================

    3.폼 헬퍼를 사용하면보기에 암시 적 play.api.i18n.Messages 매개 변수를 전달할 수 있어야합니다. 보기에에 : 당신은 (메시지 암시 적 메시지)를 추가이 작업을 수행 할 수 있습니다. 보기이된다 :

    폼 헬퍼를 사용하면보기에 암시 적 play.api.i18n.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 {
    
  4. from https://stackoverflow.com/questions/30799988/play-2-4-form-could-not-find-implicit-value-for-parameter-messages-play-api-i by cc-by-sa and MIT license