次のコントローラーメソッドがあります。
@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;
...
}
問題を回避するには?
マルチパート ファイルのこのコントローラー メソッドの変換ポリシーはどこで設定できますか?
PS
カスタムinitbinderを作成しようとしました:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(CommonsMultipartFile.class, new PropertyEditorSupport() {
@Override
public void setValue(Object file) {
setValue(new OnlyForImagesFileWrapper((MultipartFile) file));
}
});
}
しかし、フォームを送信してもこのメソッドは呼び出されず、上記のスタックトレースが表示されます。
PS
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,
...