我一直在阅读“Play for Java”一书,这本书绝对精彩.我仍然是Java的新手,但我一直在关注这些例子,我有点卡在第3章.代码可以在这里找到:Play for Java on GitHub.
问题是,当我执行boundform.get()时,表单的实际属性似乎没有进入“产品”对象.我在Eclipse的调试器中暂停了这一点,所有的值都在Form< Product>行中正确设置了. boundForm = productForm.bindFromRequest();但是当我到product.save()时它们就消失了.
我的控制器,型号,路线和表格如下所示.如果需要任何其他信息,请告诉我.
Products.java(控制器)
package controllers;
import models.Product;
import play.data.Form;
import play.mvc.Result;
import play.mvc.Controller;
import views.html.products.*;
import java.util.List;
public class Products extends Controller {
private static final Form productForm = Form.form(Product.class);
public static Result list() {
List products = Product.findAll();
return ok(list.render(products));
}
public static Result newProduct() {
return ok(details.render(productForm));
}
public static Result details(String ean) {
return TODO;
}
public static Result save() {
Form boundForm = productForm.bindFromRequest();
Product product = boundForm.get();
product.save();
return ok(String.format("Saved product %s", product));
}
}
Product.java(型号)
package models;
import java.util.ArrayList;
import java.util.List;
public class Product {
public String ean;
public String name;
public String description;
public Product() {
}
public Product(String ean, String name, String description) {
this.ean = ean;
this.name = name;
this.description = description;
}
public String toString() {
return String.format("%s - %s", this.ean, this.name);
}
private static List products;
static {
products = new ArrayList();
products.add(new Product("1111111111111", "Paperclips 1",
"Paperclips description 1"));
products.add(new Product("2222222222222", "Paperclips 2",
"Paperclips description "));
products.add(new Product("3333333333333", "Paperclips 3",
"Paperclips description 3"));
products.add(new Product("4444444444444", "Paperclips 4",
"Paperclips description 4"));
products.add(new Product("5555555555555", "Paperclips 5",
"Paperclips description 5"));
}
public static List findAll() {
return new ArrayList(products);
}
public static Product findByEan(String ean) {
for (Product candidate : products) {
if (candidate.ean.equals(ean)) {
return candidate;
}
}
return null;
}
public static List findByName(String term) {
final List results = new ArrayList();
for (Product candidate : products) {
if (candidate.name.toLowerCase().contains(term.toLowerCase())) {
results.add(candidate);
}
}
return results;
}
public static boolean remove(Product product) {
return products.remove(product);
}
public void save() {
products.remove(findByEan(this.ean));
products.add(this);
}
}
路线
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.Application.index()
GET /products/ controllers.Products.list()
GET /products/new controllers.Products.newProduct()
GET /products/:ean controllers.Products.details(ean: String)
POST /products/ controllers.Products.save()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
details.scala.html
@(productForm: Form[Product])
@import helper._
@import helper.twitterBootstrap._
@main("Product form") {
Product form
@helper.form(action = routes.Products.save()) {
Product (@productForm("name").valueOr("New"))
@helper.inputText(productForm("ean"), '_label -> "EAN")
@helper.inputText(productForm("name"),'_label -> "Name")
@helper.textarea(productForm("description"), '_label -> "Description")
}
}
我确信这是非常明显的事情.非常感谢!