gpt4 book ai didi

Java - 传递 "this"时在对象属性中获取空值

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:39 41 4
gpt4 key购买 nike

我一直在研究“Play for Java”这本书,这本书非常棒。我对 Java 还是很陌生,但我一直在关注这些示例,我有点卡在第 3 章上了。可以在此处找到代码:Play for Java on GitHub .

问题是当我执行 boundform.get() 时,表单的实际属性似乎并没有进入“产品”对象。我在 Eclipse 的调试器中暂停了它,并且所有值都正确设置在 Form<Product> boundForm = productForm.bindFromRequest(); 行但是当我到达product.save()时它们就消失了.

我的 Controller 、模型、路由和表单如下所示。如果需要任何其他信息,请告诉我。

Products.java( Controller )

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<Product> productForm = Form.form(Product.class);

public static Result list() {
List<Product> 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<Product> 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<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 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);
}
}

路线

# 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)

细节.scala.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.Application.index()">Cancel</a>
}
}

我敢肯定这是显而易见的事情。非常感谢!

最佳答案

参见 documentation处理绑定(bind)失败部分。在 Form 上调用 .get() 是不安全的,因为如果存在验证错误,它将返回 null。首选方法是先通过 hasErrors() 检查错误,然后从那里进行处理。

if (boundform.hasErrors()) {
/* There are errors somewhere in the form,
* return it to the view and display them there,
* or do whatever else you need to handle the error.
*/
return badRequest(...);
} else {
// A valid `Product`, proceed as normal.
Product product = boundform.get();

return ok(....);
}

关于Java - 传递 "this"时在对象属性中获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24367879/

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