0

我有一个提交上传文件的表格:

<form id="uploadForm" action="#" method="POST" enctype="multipart/form-data">
    <div class="instruction popup_inst">
        <span class="popup_logo">[@spring.message "pandaLogo"/]</span>

        <div class="float_right">
            <input type="button" id="cancelBtn" class="btn" onclick="closePopupFunctionality();" value="[@spring.message "cancelButton"/]" />

            <input class="btn" type="submit" id="submit" onclick="validateFileInput();" value="[@spring.message "uploadButton"/]" />
        </div>

    </div>
    <span class="popup_title">[@spring.message "uploadFile"/]</span>
    <div class="popup_container">
        [@spring.bind "assayAssessmentsRequestBean"/]
        [@spring.formInput path="assayAssessmentsRequestBean.designabilityFile.file" fieldType="file" attributes="title='path' class='upload' accept='.txt,.csv,.zip'" /]
        [@spring.formHiddenInput path="assayAssessmentsRequestBean.dateOfAssessment" attributes="" /]
        [@spring.formHiddenInput path="assayAssessmentsRequestBean.id" attributes="" /]
        [@spring.showErrors " " "error" /]
    </div>
    <input id="uploadfile" type="hidden" value="${uploadfile}"/>
</form>

此表单在控制器中点击以下方法:

@RequestMapping(value = "/uploadFile",method= RequestMethod.POST)
    public ModelAndView uploadFile(@ModelAttribute(value = "assayAssessmentsRequestBean")AssayAssessmentsRequestBean assayAssessmentsRequestBean,HttpSession session,BindingResult result) throws PanDaApplicationException {
//mycode
}

发生的事情是该方法从未被调用,当我删除 BindingResult 属性时,该方法被调用。

我尝试了另一种方法,它具有 BeanPropertyBindingResult 属性并传递要验证的目标对象,它可以工作但从不返回错误。

@RequestMapping(value = "/uploadFile",method= RequestMethod.POST)
    public ModelAndView uploadFile(@ModelAttribute(value = "assayAssessmentsRequestBean")AssayAssessmentsRequestBean assayAssessmentsRequestBean,HttpSession session/*,BindingResult result*/) throws PanDaApplicationException {
        logger.info(assayAssessmentsRequestBean.getDesignabilityFile().getFile());
        BindingResult result=new BeanPropertyBindingResult(assayAssessmentsRequestBean.getDesignabilityFile(),"designabilityFile");
4

1 回答 1

3

您应该在要验证的对象之前有 @Valid 注释,并且 BindingResult 必须紧跟在经过验证的对象之后

所以这应该做的工作

 public ModelAndView  uploadFile(@Valid @ModelAttribute(
    value = "assayAssessmentsRequestBean")AssayAssessmentsRequestBean   assayAssessmentsRequestBean,BindingResult result,
HttpSession session) {

 }

那么您应该定义自己的验证器并将其设置为活页夹。验证器将被自动调用。但我不确定您是否可以通过这种方式对多部分进行验证。

于 2013-04-10T14:03:36.937 回答