我正在尝试使用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")
}
}
以下是我的观点。
@(sku: String, productForm: Form[controllers.Product])
@helper.form(action = routes.Products.update(sku)) {
@helper.inputText(productForm("sku"))
@helper.inputText(productForm("title"))
}
当我编译这个程序时,我收到了以下错误
[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
任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
正如@ jirka-helmich指出的那样,@ jerry的解决方案已被弃用。
Play 2.5.x documentation列出了以下解决方案(请注意I18nSupport特性):
import javax.inject.Inject
import play.api.i18n.I18nSupport
class MyController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
// ...
有关详细信息,请参阅this answer。
答案 1 :(得分:0)
当您使用 inputText 时,需要“隐含消息”。通过导入以下语句添加它
import play.api.Play.current
import play.api.i18n.Messages.Implicits._
在视图中,您需要添加以下代码
@(sku: String, productForm: Form[controllers.Product])(implicit messages: Messages)
参考Play 2.4: Form: could not find implicit value for parameter messages: play.api.i18n.Messages