[英]How to execute code before @InitBinder validation
I have a program that has a functionality to upload a file and then validate if it's name format is correct and save it to db.我有一个程序,它具有上传文件的功能,然后验证它的名称格式是否正确并将其保存到 db。
In my main controller I am using @InitBinder for validation.在我的主控制器中,我使用 @InitBinder 进行验证。
@InitBinder("uploadFileForm")
protected void initBinderUploadForm(WebDataBinder binder) {
binder.setValidator(fileNameFormatValidator);
}
In my validator method I am using this code snippet:在我的验证器方法中,我使用了以下代码片段:
static void validateFileName(String fileUploadKey, MultipartFile file, Errors errors) {
Matcher validFilenameMatcher = VALID_FILE_NAME.matcher(file.getOriginalFilename());
if (!validFilenameMatcher.matches()) {
errors.rejectValue(fileUploadKey, null, null, SOME_REGEX);
}
}
What I want to do is, I want to format file name (like replacing some characters in file name) and then use validator class.我想要做的是,我想格式化文件名(比如替换文件名中的一些字符),然后使用验证器类。 Thus I need to change file name before the validation.
因此我需要在验证之前更改文件名。
How can I achieve editing file name before validating the format with @InitBinder ?如何在使用@InitBinder验证格式之前实现编辑文件名?
Edit: Noone answers?编辑:没有人回答? Or the question isn't clear ?
还是问题不清楚?
Why not use WebDataBinder
's addCustomFormatter like you did for adding a validator ?为什么不像添加验证器那样使用
WebDataBinder
的addCustomFormatter呢?
@InitBinder("uploadFileForm")
protected void initBinderUploadForm(WebDataBinder binder) {
binder.addCustomFormatter(fileNameFormatter);
binder.setValidator(fileNameFormatValidator);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.