0
我是新玩家!我遇到了一些總是有錯誤的表單。即使所有字段都填滿了,我也無法弄清楚問題所在。玩!表格總是有錯誤
路線
GET /products/ controllers.Products.list()
GET /products/new controllers.Products.newProduct()
POST /products/ controllers.Products.save()
產品的Controller.java
import play.data.Form;
private final static Form<Product> productForm = form(Product.class);
public static Result list() {
List<Product> productList = Product.findAll();
return ok(list.render(productList));
}
public static Result newProduct() {
return ok(details.render(productForm));
}
public static Result save() {
Form<Product> boundForm = productForm.bindFromRequest();
if(boundForm.hasErrors()) {
flash("error",
"Please correct the form below.");
return badRequest(details.render(boundForm));
}
// For mystery reasons, in this line, product is always null
// Product product = boundForm.get();
Product product = new Product();
product.ean = boundForm.data().get("ean");
product.name = boundForm.data().get("name");
product.description = boundForm.data().get("description");
product.save();
flash("success",
String.format("Successfully added product %s", product));
return redirect(routes.Products.list());
}
產品的Model.java
import static play.data.validation.Constraints.Required;
public class Product {
@Required
public String ean;
@Required
public String name;
public String description;
...
}
產品的form.scala.html
@(productForm: Form[Product])
@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>
}
}
這裏是調試器的截圖,有數據和錯誤太:(
我在做什麼錯?
~~~~更新~~~~~
我增加了列表路由和控制器動作
這裏是回購:
https://github.com/LTroya/up-and-running-play
你錯過了一些文件'潰敗es.Products.list'沒有在'routes'和controller中定義,main.scala.html缺失。十分鐘內從頭開始複製是很困難的。 –
@AndrzejJozwik完成。我添加了列表路由,處理列表的控制器操作以及github存儲庫 – LTroya