我有以下控制器方法:
@RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
public String saveTerminal( @RequestParam(value = "name") String name,
@RequestParam(value = "file") @Valid OnlyForImagesFileWrapper file,
BindingResult bindingResult )
{
...
我看到以下堆栈跟踪:
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'com.terminal.domain.validation.OnlyForImagesFileWrapper'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
....
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
... 71 more
OnlyForImagesFileWrapper 源码:
public class OnlyForImagesFileWrapper {
@Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
private MultipartFile multipartFile;
...
}
如何避免问题?
我在哪里可以为此控制器方法为多部分文件设置转换策略?
附言
我尝试编写我的自定义 initbinder:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(CommonsMultipartFile.class, new PropertyEditorSupport() {
@Override
public void setValue(Object file) {
setValue(new OnlyForImagesFileWrapper((MultipartFile) file));
}
});
}
但是当我提交表单并且我看到上面提到的堆栈跟踪时,这个方法不会调用。
附言
M. Deinum指令执行后的结果(当我在saveTerminal
方法中时):
我还注意到我的 initbinder 方法没有调用。
有关我的代码的更多详细信息(M. Denium 建议之后的状态):
jsp:
<input type="file" id="newFile" name="file" class="file" size="21.5" accept=".jpg,.png,.gif,.bmp" style="opacity: 0;">
控制器方法的参数:
...
@ModelAttribute @Valid OnlyForImagesFileWrapper wrapper,
BindingResult bindingResult,
...