Java代码  收藏代码
  1. @RequestMapping(method = RequestMethod.POST)  
  2. public String save(  
  3.         @RequestParam(value = "file", required = false) MultipartFile file,  
  4.         @Valid Store store, BindingResult result) {  
  5.     if (result.hasErrors()) {  
  6.         return "admin/store/create";  
  7.     }  
  8.     if (!file.isEmpty()) {  
  9.         try {  
  10.             byte[] bytes = file.getBytes();  
  11.             String path = UploadFileUtil.saveFile(bytes);  
  12.             store.setLogo(path);  
  13.         } catch (IOException e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.     }  
  17.     store.setCreateTime(new Date());  
  18.     storeService.save(store);  
  19.     return "redirect:/admin/store/" + store.getId();  
  20. }  




网上看到的都是一个文件上传的例子,实际很多时候是与model一起提交上来的,今天试了一下,发现这2个参数的位置有讲究,换一下,就出错了。大致的错误是验证了错误,但是不能正常的返回提交页面,显示错误。具体原因没有调查 

Java代码  收藏代码
  1. @RequestParam(value = "file", required = false) MultipartFile file  
  2. @Valid Store store  



Html代码  收藏代码
    1. <html>   
    2.       
    3.     <head>   
    4.         <title >layout title</title>   
    5.     </head>   
    6.       
    7.     <body >   
    8.         <form action="/admin/store" method="post" enctype="multipart/form-data">   
    9.       
    10. <fieldset>   
    11.     <legend >   
    12.         添加    </legend>   
    13.     <p>   
    14.     <label >名称:</label>   
    15.                         <input type="text" id="name" name="name" value="" >          </p>   
    16.       
    17.     <p>   
    18.         <label >域名: </label>   
    19.                             <input type="text" id="domainName" name="domainName" value="" >       </p>   
    20.       
    21.     <p>   
    22.         <label >地址: </label>   
    23.                         <input type="text" id="address" name="address" value="" >         </p>   
    24.       
    25.     <p>   
    26.         <label >商标: </label>   
    27.         <input type="file" name="file"/>   
    28.     </p>   
    29.       
    30.     <p>   
    31.         <label >&nbsp;</label>   
    32.         <input type="submit" value="添加"/>   
    33.     </p>   
    34.       
    35. </fieldset>   
    36.    
    37. </form>   
    38.     </body>   
    39. </html>   
    40.