gpt4 book ai didi

java - PlayFramework (2.5.x) 不绑定(bind)来自表单的数据,返回带有空字段的 Ojbect

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:39:59 24 4
gpt4 key购买 nike

问题是将数据从表单(或参数映射)绑定(bind)到实际的表单对象。我添加了一些 println仅用于测试目的。这是 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());
}

}

此处为简单起见class Product只是三个公共(public)领域:

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);
}


}

这里是 Play 模板(没有 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>
}

}

除了我得到带有空字段的 Product 对象外,一切都很顺利,但是 field(fieldName).value()data()返回正确的数据。看起来 Form 的行为与 DynamicForm 相同。当我使用 bind(Map<String,String>) 时开始发生奇怪的事情而不是 bindFromRequest() - 同样的结果。控制台的结果是(我使用 testEAN 填写表单“ean”字段)

    >>Bound ean form data: testEAN->Product from bound form: null - null
>>Filled ean form data: null->Product from filled form: 0000 - 0000

最佳答案

需要在 Product class 中添加属性访问器/修改器 (getXXX/setXXX)。这是关键点

关于java - PlayFramework (2.5.x) 不绑定(bind)来自表单的数据,返回带有空字段的 Ojbect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com