[英]PlayFramework (2.5.x) doesn't bind data from form returning Ojbect with null fields
The problem is to bind data from form (or from Map of parameters) to actual Form object. 问题是将数据从表单(或从参数Map)绑定到实际的Form对象。 I have added some
println
just for testing purposes. 我添加了一些
println
仅用于测试目的。 Here is code for Controller class. 这是Controller类的代码。
package controllers;
import java.util.List;
import javax.inject.Inject;
import models.Product;
import play.data.Form;
import play.data.FormFactory;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.products.list;
import views.html.products.details;
public class Products extends Controller {
@Inject
public Products(FormFactory formFactory) {
productForm = formFactory.form(Product.class);
}
private static Form<Product> productForm;
public Result list(){
List<Product> products = Product.findAll();
return ok(list.render(products));
}
public Result newProduct(){
return ok(details.render(productForm));
}
public Result save(){
Form<Product> filledForm=productForm.fill(new Product("0000","0000","0000"));
Form<Product> boundForm=productForm.bindFromRequest();
final Product product =(Product) boundForm.get();
System.out.println(">>Bound ean form data: "+boundForm.field("ean").value()+"->Product from bound form: "+boundForm.get());
System.out.println(">>Filled ean form data: "+filledForm.field("ean").value()+"->Product from filled form: "+filledForm.get());
//product.save();
flash("success",String.format("Successfully added product %s", product));
return redirect(routes.Products.list());
}
}
Here for the purpose of simplicity class Product
is just of three public fields: 这里为了简化
class Product
的目的, class Product
只是三个公共领域:
package models;
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", ean, name);
}
}
And here is Play template( without main
wrapper template that take care of <head> <body>
stuff) 这里是播放模板(没有
main
包装模板,负责<head> <body>
东西)
@(productForm: Form[Product])
@import helper._
@main("Product form") {
<div class="main">
<h1>Product form</h1>
@helper.form(action = routes.Products.save(),'_class -> "form-group") {
<fieldset>
<legend>Product (@productForm("name").valueOr("New"))</legend>
@helper.inputText(productForm("ean"), '_label -> "EAN",'_class->"input")
@helper.inputText(productForm("name"),'_label -> "Name",'_class->"input")
@helper.textarea(productForm("description"), '_label -> "Description",'_class->"input")
</fieldset>
<input type="submit" class="btn btn-success" value="Save">
<a class="btn btn-warning" href="@routes.Products.index()">Cancel</a>
}
} }
Everything rides nice and smooth except that I get Product object with null fields, but field(fieldName).value()
or data()
return correct data. 除了我得到具有空字段的Product对象外,所有东西都很顺利,但是
field(fieldName).value()
或data()
返回正确的数据。 Looks like Form behaves as DynamicForm. 看起来Form表现为DynamicForm。 Strange things starting to happen when I use
bind(Map<String,String>)
instead of bindFromRequest()
- same result both. 当我使用
bind(Map<String,String>)
而不是bindFromRequest()
时,奇怪的事情开始发生 - 两者都是相同的结果。 And resul from console is (i have used testEAN filling the form "ean" field) 从控制台得到的结果是(我用testEAN填写了表格“ean”字段)
>>Bound ean form data: testEAN->Product from bound form: null - null
>>Filled ean form data: null->Product from filled form: 0000 - 0000
Need to add property accessors/mutators ( getXXX/setXXX
) in Product class
. 需要在
Product class
添加属性访问器/ mutator( getXXX/setXXX
)。 That is crucial point 这是至关重要的一点
You have to provide the type of the form 您必须提供表单的类型
Form<Product> boundForm = factoryForm.form(Product.class).bindFromRequest();
Product product = boundForm.get();
If you do not provide the type of the object you are biding how play will infer the fields and properties?! 如果您没有提供对象的类型,那么游戏将如何推断字段和属性?!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.