2016-05-22 3 views
0

Ich versuche die Beispielpfadvariablen in meinem lokalen mit Play Framework 2.5.3 zu testen.konnte keinen impliziten Wert für Parametermeldungen finden: play.api.i18n.Messages in Play Framework 2.5.3

package controllers 

import play.api._ 
import play.api.data._ 
import play.api.data.Forms._ 
import play.api.mvc._ 
import play.api.libs.json._ 
import play.api.libs.json.Json._ 
import play.api.libs.functional.syntax._ 

case class Product(sku: String, title: String) 
object Product { 
implicit def pathBinder(implicit stringBinder: PathBindable[String]) = new PathBindable[Product] { 
    override def bind(key: String, value: String): Either[String, Product] = { 
    for { 
     sku <- stringBinder.bind(key, value).right 
     product <- productMap.get(sku).toRight("Product not found").right 
    } yield product 
    } 
    override def unbind(key: String, product: Product): String = { 
    stringBinder.unbind(key, product.sku) 
    } 
} 

def add(product: Product) = productMap += (product.sku -> product) 

val productMap = scala.collection.mutable.Map(
    "ABC" -> Product("ABC", "8-Port Switch"), 
    "DEF" -> Product("DEF", "16-Port Switch"), 
    "GHI" -> Product("GHI", "24-Port Switch") 
) 
} 

object Products extends Controller { 
    implicit val productWrites = new Writes[Product] { 
    def writes(product: Product) = Json.obj(
    "sku" -> product.sku, 
    "title" -> product.title 
    ) 
    } 

implicit val productReads: Reads[Product] = (
(JsPath \ "sku").read[String] and 
(JsPath \ "title").read[String] 
)(Product.apply _) 

val productForm: Form[Product] = Form(
    mapping(
    "sku" -> nonEmptyText, 
    "title" -> nonEmptyText 
)(Product.apply)(Product.unapply) 
) 

def index = Action { 
    Ok(toJson(Product.productMap.values)) 
} 

def postProduct = Action(BodyParsers.parse.json) { request => 
    val post = request.body.validate[Product] 
    post.fold(
    errors => { 
     BadRequest(Json.obj("status" ->"error", "message" -> JsError.toFlatJson(errors))) 
    }, 
    product => { 
     Product.add(product) 
     Created(toJson(product)) 
    } 
) 
} 

def edit(product: Product) = Action { 
    Ok(views.html.products.form(product.sku, productForm.fill(product))) 
} 

def update(sku: String) = Action { 
    Ok("Received update request") 
} 

} 

Und unten ist meine Ansicht.

@(sku: String, productForm: Form[controllers.Product]) 

    @helper.form(action = routes.Products.update(sku)) { 
    @helper.inputText(productForm("sku")) 
    @helper.inputText(productForm("title")) 
    } 

Wenn ich dieses Programm bin ich Kompilieren unter Störung erhalte

[error] /home/rajkumar/CodeBase/workspaceEclipse/PFC2/app/views/products/form.scala.html:4: could not find implicit value for parameter messages: play.api.i18n.Messages 
    [error] @helper.inputText(productForm("sku")) 
    [error]     ^
    [error] /home/rajkumar/CodeBase/workspaceEclipse/PFC2/app/views/products/form.scala.html:5: could not find implicit value for parameter messages: play.api.i18n.Messages 
    [error] @helper.inputText(productForm("title")) 
    [error]     ^
    [error] 9 errors found 
    [error] (compile:compileIncremental) Compilation failed 

Könnte jemand mir helfen, dieses Problem zu lösen.

Antwort

0

Wenn Sie inputText verwenden, werden die 'impliziten Nachrichten' benötigt. Hinzufügen es durch den Import der folgenden Aussagen

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

In den Ansichten benötigen Sie den folgenden Code

@(sku: String, productForm: Form[controllers.Product])(implicit messages: Messages) 

Referenz Play 2.4: Form: could not find implicit value for parameter messages: play.api.i18n.Messages

+0

Muss ich diese Pakete in Controller oder in views.html hinzufügen –

+0

@RajkumarNatarajan Nein, Sie müssen das Paket nicht in die Ansicht hinzufügen, aber Sie müssen hinzufügen "implizite Nachrichten" in der Ansicht, ich habe die Antwort aktualisieren Viel Glück – Jerry

+1

Dies ist veraltet seit Play 2.5 (speziell play.api.Play.current), siehe die referenzierte Frage für die richtige Antwort. –

1

Wie @ jirka-Helmich wies darauf hin, die Lösung mit Hilfe @ Jerry ist veraltet.

Die Play 2.5.x documentation folgende Lösung führt (man beachte die I18nSupport Merkmal):

import javax.inject.Inject 
import play.api.i18n.I18nSupport 
class MyController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport { 
// ... 

Siehe this answer für weitere Details.

Verwandte Themen