1

When I haven't fileupload tag(component), my code run fine. But when I add file input(component), when I submit, page only refresh, form is resseted, feedbackPanel don't show anything. I spent a day for this error, but still resolve it. Here is my code:

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>
luanvu
  • 89
  • 10

1 Answers1

0

There must be an error in your logs. I'd bet the error is that the FileUploadField has null model and thus Wicket has no where to store the uploaded data.

martin-g
  • 17,243
  • 2
  • 23
  • 35