Domanda

I have a form that I submitted to upload file:

<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>

this form hits the following method in controller:

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

what happen is that the method never called and when I remove the BindingResult attribute the method is invoked.

I tried another approach which is having BeanPropertyBindingResult attribute and pass target object to be validated and it works but never return errors.

@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");
È stato utile?

Soluzione

you should have @Valid annotation before the object you vant to validate and BindingResult must follow immediately after the validated object

so this should do the job

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

 }

then you should define your own Validator and set it to the binder. The Validator will be invoked automatically. But I am not sure if you can do validation on multipart in this way.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top