我目前有一个采用MultipartFile的Spring MVC控制器
@RequestMapping(method = RequestMethod.POST) public String doUpload(@RequestParam("file") final MultipartFile file) { /* ... */ }
该文件包含csv数据,每行将使用该数据创建域对象列表。可以了
我已经为行数据编写了一个转换器:
class MyObjectConverter implements org.springframework...Converter<String[], MyObject> { /* ... */ }
还有一个文件验证器
class UploadFileValidator implements org.springframework.validation.Validator { /* ... */ }
我有一个表格可以上传:
<form method="post" action="<@spring.url '/upload'/>" enctype="multipart/form-data"> <input id="upload" type="file" name="file"/> <input type="submit" id="uploadButton"/> </form
但是我真正想做的是将它们捆绑在一起,以便我的控制器可以使用类似
@RequestMapping(method = RequestMethod.POST) public String doUpload( @Valid final List<MyObject> objList, final BindingResult result) { ...}
我知道Spring MVC框架支持转换器和验证器,但是我无法理解如何使它们协同工作。
首先,我将MultipartFile包装在一个表单支持对象中:
public class UploadBackingForm { private MultipartFile multipartFile; /* ... getter/setter */ }
然后我将其绑定到表单:
<form method="post" enctype="multipart/form-data"> <@spring.bind "backingform.*"/> <tr> <td><@spring.formInput 'backingform.multipartFile' '' 'file' /></td> <td> <button type="submit">Upload</button> </td> </tr> </form>
在控制器中,我分配一个验证器:
@InitBinder public void initBinder(final DataBinder binder) { binder.setValidator(new UploadValidator()); }
这是验证者:
public class UploadValidator implements Validator { private final Converter<String[], MyObject> converter = new MyObjectConverter(); @Override public boolean supports(final Class<?> clazz) { return UploadBackingForm.class.equals(clazz); } @Override public void validate(final Object target, final Errors errors) { final UploadBackingForm backingForm = (UploadBackingForm) target; final MultipartFile multipartFile = backingForm.getMultipartFile(); final List<String[]> uploadData = /* parse file */ for (final String[] uploadDataRow : uploadData){ try { converter.convert(uploadDataRow); } catch (IllegalArgumentException e) { errors.rejectValue("multipartFile", "line.invalid", ...); } } } }
验证器使用Converter将行项目转换为MyObj。
现在,doPost方法如下所示:
@RequestMapping(method = RequestMethod.POST) public String doUpload( @Valid @ModelAttribute("backingform") UploadBackingForm backingForm, final BindingResult result, final HttpSession session) throws IOException { final UploadConverter converter = new UploadConverter(); final List<MyObj> imports = converter.convert(backingForm.getMultipartFile().getInputStream()); }
UploadConverter与UploadValidator大致相同:
public class UploadConverter implements Converter<InputStream, List<MyObject>> { private final Converter<String[], MyObject> converter = new MyObjectConverter(); @Override public List<MyObject> convert(final InputStream source) { final List<String[]> detailLines = /* ... getDetailLines */ final List<MyObject> importList = new ArrayList<MyObject>(detailLines.size()); for (final String[] row : detailLines) { importList.add(converter.convert(row)); } return importList; } }
唯一的问题是验证和转换过程几乎是同一回事。幸运的是,上传文件不会很大,因此重复工作并不是大问题。