- @RequestMapping(method = RequestMethod.POST)
- public String save(
- @RequestParam(value = "file", required = false) MultipartFile file,
- @Valid Store store, BindingResult result) {
- if (result.hasErrors()) {
- return "admin/store/create";
- }
- if (!file.isEmpty()) {
- try {
- byte[] bytes = file.getBytes();
- String path = UploadFileUtil.saveFile(bytes);
- store.setLogo(path);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- store.setCreateTime(new Date());
- storeService.save(store);
- return "redirect:/admin/store/" + store.getId();
- }
网上看到的都是一个文件上传的例子,实际很多时候是与model一起提交上来的,今天试了一下,发现这2个参数的位置有讲究,换一下,就出错了。大致的错误是验证了错误,但是不能正常的返回提交页面,显示错误。具体原因没有调查
- @RequestParam(value = "file", required = false) MultipartFile file
- @Valid Store store
- <html>
- <head>
- <title >layout title</title>
- </head>
- <body >
- <form action="/admin/store" method="post" enctype="multipart/form-data">
- <fieldset>
- <legend >
- 添加 </legend>
- <p>
- <label >名称:</label>
- <input type="text" id="name" name="name" value="" > </p>
- <p>
- <label >域名: </label>
- <input type="text" id="domainName" name="domainName" value="" > </p>
- <p>
- <label >地址: </label>
- <input type="text" id="address" name="address" value="" > </p>
- <p>
- <label >商标: </label>
- <input type="file" name="file"/>
- </p>
- <p>
- <label > </label>
- <input type="submit" value="添加"/>
- </p>
- </fieldset>
- </form>
- </body>
- </html>