Wicket Custom错误消息

时间:2012-12-22 09:12:31

标签: java wicket feedbackpanel

我有一个上传字段。我只想上传图片。我已经应用了相同的验证。但是在出错时我希望错误应该添加到表单上的feedbackpanel。

UploadPanel.html

<form wicket:id="frmProduct" >
        <div wicket:id="feedback"></div>
        <label>Category1 *:</label><br/>
        <input type="text" wicket:id="Category1"><br/>
        <label>Category2 *:</label><br/>
        <input type="text" wicket:id="Category2"><br/>
        <label>ProductName *:</label><br/>
        <input type="text" wicket:id="ProductName"><br/>
        <label>TaxAmount *:</label><br/>
        <input type="text" wicket:id="TaxAmount"><br/>
        <label>UnitPrice *:</label><br/>
        <input type="text" wicket:id="UnitPrice"/><br/>
        <label>Description</label><br/>
        <textarea wicket:id="Description" id ="Description" rows="6" cols="20"></textarea><br/>
        <label>Description</label><br/>
        <input wicket:id="uploadField" size="40" type="file"/><br/>
        <input type="submit" wicket:id="submit" value="Save"/>
</form>

UploadPanel.java

                    if(uploadField.getFileUpload() != null && uploadField.getFileUpload().getClientFileName() != null){
                    FileUpload upload = uploadField.getFileUpload();
                    String ct = upload.getContentType();

                    if (!imgctypes.containsKey(ct)) {
                        hasError = true;
                    }

                    if(upload.getSize() > maximagesize){
                        hasError = true;
                    }

                    if(hasError == false){
                        System.out.println("######################## Image can be uploaded ################");
                        imageEntry.setContentType(upload.getContentType());
                        imageEntry.setImageName(upload.getClientFileName());
                        imageEntry.setImageSize(upload.getSize());
                        if(imageEntry != null){
                            try {
                                save(imageEntry,upload.getInputStream());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }else{
                        target.appendJavaScript("$().toastmessage('showNoticeToast','Please select a valid image!!')");
                        System.out.println("#################### Error in image uploading ###################");
                    }
                }else{
                    System.out.println("########################### Image not Selected #####################");
                }

任何帮助和建议表示赞赏!提前谢谢。

1 个答案:

答案 0 :(得分:2)

FileUploadField是表单组件,因此您只需执行

即可
fileUploadField.error("error with file upload");

PS:而不是

if(hasError == false)

做一个

if (!hasError)

更好:)