gpt4 book ai didi

spring - org.springframework.validation.BeanPropertyBindingResult 异常

转载 作者:行者123 更新时间:2023-12-04 23:50:00 24 4
gpt4 key购买 nike

嗨,我是 Spring 框架的新手。我做了一个小例子,我尝试使用 spring 验证 api 验证我的输入字段。这是代码

@RequestMapping(value = "/applicationFormSubmit", method = RequestMethod.POST)
public String insertdata( @ModelAttribute("applicationForm") @Valid ApplicationFormBean applicationFormBean, @RequestParam("file") MultipartFile file, BindingResult result,Model model)
{
if(result.hasErrors())
{

return "applicationForm";
}
try {
Blob blob = Hibernate.createBlob(file.getInputStream());

// applicationFormBean..setFilename(file.getOriginalFilename());
applicationFormBean.setSignature(blob);
// applicationFormBean.setContentType(file.getContentType());
} catch (IOException e) {
e.printStackTrace();
}
applicationFormUserService.insertApplicationData(applicationFormBean);

return "applicationForm";

}

但是当我用空白值提交 from 时,它给了我以下错误
Field error in object 'applicationForm' on field 'applicantName': rejected value []; codes [NotEmpty.applicationForm.applicantName,NotEmpty.applicantName,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [applicationForm.applicantName,applicantName]; arguments []; default message [applicantName]]; default message [Please enter your nnnn.]

Field error in object 'applicationForm' on field 'applicantName': rejected value []; codes [Size.applicationForm.applicantName,Size.applicantName,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [applicationForm.applicantName,applicantName]; arguments []; default message [applicantName],20,2]; default message [this is worng ]

最佳答案

请更改代码中的行,如下所示。

public String insertdata(
@ModelAttribute("applicationForm") @Valid ApplicationFormBean applicationFormBean,
BindingResult result,
Model model,
@RequestParam("file") MultipartFile file)

this Spring MVC tutorial 中所述:

The BindingResult must come right after the model object that is validated or else Spring will fail to validate the object and throw an exception.



This tutorial也提到了这个问题。

关于spring - org.springframework.validation.BeanPropertyBindingResult 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802681/

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