您好我是Spring框架的新手。我做了一个小例子,我尝试使用spring validation 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 ]
答案 0 :(得分:6)
plz更改代码中的行,如下所示......
public String insertdata(
@ModelAttribute("applicationForm") @Valid ApplicationFormBean applicationFormBean,
BindingResult result,
Model model,
@RequestParam("file") MultipartFile file)