[英]How to execute code before @InitBinder validation
我有一個程序,它具有上傳文件的功能,然后驗證它的名稱格式是否正確並將其保存到 db。
在我的主控制器中,我使用 @InitBinder 進行驗證。
@InitBinder("uploadFileForm")
protected void initBinderUploadForm(WebDataBinder binder) {
binder.setValidator(fileNameFormatValidator);
}
在我的驗證器方法中,我使用了以下代碼片段:
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);
}
}
我想要做的是,我想格式化文件名(比如替換文件名中的一些字符),然后使用驗證器類。 因此我需要在驗證之前更改文件名。
如何在使用@InitBinder驗證格式之前實現編輯文件名?
編輯:沒有人回答? 還是問題不清楚?
為什么不像添加驗證器那樣使用WebDataBinder
的addCustomFormatter呢?
@InitBinder("uploadFileForm")
protected void initBinderUploadForm(WebDataBinder binder) {
binder.addCustomFormatter(fileNameFormatter);
binder.setValidator(fileNameFormatValidator);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.