我正在尝试构建一个简单的crud表单。我正在关注java书籍中的例子。问题是,当我提交表单时,它会创建一个新的产品对象,但是产品对象中的值设置为NULL。它们未设置为表单中填充的值。换句话说,表单似乎没有绑定到产品类。
输出
Saved product null - null
表单视图html
@(productForm: Form[Product])
@import helper._
@import helper.twitterBootstrap._
@main("Product form") {
<h1>Product form</h1>
@helper.form(action = routes.Products.save()) {
<fieldset>
<legend>Product (@productForm("name").valueOr("New"))</legend>
@helper.inputText(productForm("ean"), '_label -> "EAN")
@helper.inputText(productForm("name"),'_label -> "Name")
@helper.textarea(productForm("description"), '_label -> "Description")
</fieldset>
<input type="submit" class="btn btn-primary" value="Save">
<a class="btn" href="@routes.Products.list()">Cancel</a>
}
}
控制器类
package controllers;
import java.util.List;
import models.Product;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.products.list;
import views.html.products.details;
public class Products extends Controller {
public static Form<Product> productForm = Form.form(Product.class);
public static Result index() {
return ok();
}
public static Result list() {
List<Product> products = Product.findAll();
return ok(list.render(products));
}
public static Result save() {
Form<Product> boundForm = productForm.bindFromRequest();
Product product = boundForm.get();
product.save();
return ok(String.format("Saved product %s", product));
}
public static Result newProduct() {
return ok(details.render(productForm));
}
public static Result details(String ean) {
return TODO;
}
}
产品类
package models;
import java.util.ArrayList;
import java.util.List;
public class Product {
public String ean;
public String name;
public String description;
private static List<Product> products;
static {
products = new ArrayList<Product>();
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 Product() {
}
public Product(String ean, String name, String description) {
this.ean = ean;
this.name = name;
this.description = description;
}
public static List<Product> findAll() {
return new ArrayList<Product>(products);
}
public static Product findByEan(String ean) {
for (Product candidate : products) {
if (candidate.ean.equals(ean)) {
return candidate;
}
}
return null;
}
public static List<Product> findByName(String term) {
final List<Product> results = new ArrayList<Product>();
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);
}
public String toString() {
return String.format("%s - %s", ean, name);
}
}
答案 0 :(得分:0)
显然正在运行play clean
修复它。但我不知道为什么。