gpt4 book ai didi

java - 文件上传字段 Wicket 错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:50 25 4
gpt4 key购买 nike

当我没有文件上传标签(组件)时,我的代码运行良好。但是当我添加文件输入(组件)时,当我提交时,页面仅刷新,表单被重置,反馈面板不显示任何内容。这个错误我花了一天时间,还是解决了。这是我的代码:

AddProductPanel.java

public class AddProductPanel extends Panel {
private FileUploadField imageUploadField;
private static final String UPLOAD_FOLDER = "/Users/luanvu/Data/";
private Product product;

public AddProductPanel(String id) {
super(id);
List<Categories> categoriesList = new CategoriesHome().listAll();
product = new Product();
final ProductHome productHome=new ProductHome();
add(new FeedbackPanel("feedbackMessage"));
Form<Product> form = new Form<Product>("addProductForm"){
@Override
protected void onSubmit() {
super.onSubmit();
File f=uploadFiel(imageUploadField.getFileUpload());
if(f==null){
error("Upload file fail");
return;
}
product.setImg(f.getAbsolutePath());
productHome.attachDirty(product);
setResponsePage(ListProduct.class);
}
};
form.setMultiPart(true);
form.setMaxSize(Bytes.kilobytes(100));
form.add(new DropDownChoice<Categories>("category",
new PropertyModel<Categories>(product, "categories"),
categoriesList, new ChoiceRenderer<Categories>("name", "id")));
form.add(new RequiredTextField<String>("name", new PropertyModel<String>(product, "name")));
form.add(new RequiredTextField<Float>("price", new PropertyModel<Float>(product, "price")).add(RangeValidator.range(0.0f, null)));
form.add(imageUploadField=new FileUploadField("imageUpload"));
add(form);
}
private File uploadFiel(final FileUpload uploadedFile ){
if (uploadedFile != null) {
File newFile = new File(UPLOAD_FOLDER + uploadedFile.getClientFileName());
if (newFile.exists()) {
newFile.delete();
}

try {
newFile.createNewFile();
uploadedFile.writeTo(newFile);
info("saved file: " + uploadedFile.getClientFileName());
return newFile;
} catch (Exception e) {
throw new IllegalStateException("Error");
}
}
return null;
}}

AddProductPanel.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<wicket:panel>
<div style="color: red" wicket:id="feedbackMessage"></div>
<div style="margin: auto; width: 80%;">
<form id="addProductForm" method="get" wicket:id="addProductForm">
<fieldset id="add-product" class="center">
<legend>Add Product</legend>
<p>
<span>Category</span> <select wicket:id="category">
</select>
</p>
<p>
<span>Name: </span><input type="text" id="name" wicket:id="name" />
</p>
<p>
<span>Price: </span><input type="text" id="price"
wicket:id="price" />
</p>
<p>
<span>Image: </span><input type="file" wicket:id="imageUpload" />
</p>
<p>
<input type="submit" name="add" value="Add" />
</p>
</fieldset>
</form>
</div>
</wicket:panel>
</body>
</html>

最佳答案

您的日志中一定有错误。我敢打赌错误是 FileUploadField 具有 null 模型,因此 Wicket 没有地方存储上传的数据。

关于java - 文件上传字段 Wicket 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28784942/

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