Fileupload Field Wicket错误

时间:2015-02-28 17:55:10

标签: java file-upload wicket-1.6

当我没有fileupload标签(组件)时,我的代码运行正常。但是当我添加文件输入(组件)时,当我提交时,页面只刷新,表单被重新发布,feedbackPanel不显示任何内容。我花了一天时间来解决这个错误,但仍然可以解决它。这是我的代码:

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

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

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