gpt4 book ai didi

java - 带有 BindingResult 属性的 Spring Validation 永远不会命中目标 Controller 方法

转载 作者:行者123 更新时间:2023-12-01 23:53:48 26 4
gpt4 key购买 nike

我提交了一份用于上传文件的表单:

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

此表单在 Controller 中调用以下方法:

@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");

最佳答案

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

所以这应该可以完成工作

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

}

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

关于java - 带有 BindingResult 属性的 Spring Validation 永远不会命中目标 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15927884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com