我有一个提交上传文件的表格:
<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");