2012-02-13 20 views
8

Sto utilizzando Spring MVC 3. Ho cercato di accedere agli attributi del file caricato ma continuo a ricevere il seguente messaggio di errore. Posso accedere agli altri campi del modulo che viene pubblicato ma non riesco ad accedere al file caricato.Caricamento file in primavera 3 MVC - Eccezione puntatore nullo

nullhandleForm - Failed to convert property value of type 'java.lang.String' to required type 
'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'file'; 
nested exception is java.lang.IllegalStateException: 
Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile] 
for property 'file': no matching editors or conversion strategy found 

HTML/file JSP

<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 
    <p>${Response}</p> 
    <h1>Upload Songs</h1>   
    <table> 
     <form:form action="" commandName="handleForm"> 
     <tr>     
      <td>Song Name :</td> 
      <td><form:input path="songName"/></td>           
     </tr> 
     <tr> 
      <td>Artist Name :</td>       
      <td><form:input path="artistName"/></td> 
     </tr> 
     <tr> 
      <td>Gendre :</td>       
      <td><form:input path="gendre"/></td> 
     </tr> 
     <tr> 
      <td>description :</td> 
      <td><form:textarea path="description"/></td> 

     </tr> 
     <tr> 
      <td>Browse File :</td> 
      <td><form:input type="file" path="file" /></td> 
     </tr> 
     <tr> 
      <td colspan="2" style="text-align: center"><input type="submit" value="submit" /></td>       
     </tr>    
     </form:form> 
    </table>   
</body> 

La forma handlerclass

public class HandleForm { 

private String songName; 
private String artistName; 
private String gendre; 
private String description;  
private CommonsMultipartFile file; 


public CommonsMultipartFile getFile() { 
    return file; 
} 

public void setFile(CommonsMultipartFile file) { 
    this.file   = file;  
} 

public String getArtistName() { 
    return artistName; 
} 

public void setArtistName(String artistName) { 
    this.artistName = artistName; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getGendre() { 
    return gendre; 
} 

public void setGendre(String gendre) { 
    this.gendre = gendre; 
} 

public String getSongName() { 
    return songName; 
} 

public void setSongName(String songName) { 
    this.songName = songName; 
} 

} 

Il controller

@Controller 
public class AdminController{ 

@RequestMapping(value = "/admin", method = RequestMethod.GET) 
public String showAdmin(){ 
    return "admin/index"; 
} 


@RequestMapping(value = "/admin/upload-songs", method = RequestMethod.GET) 
public String showContacts(Model model) { 
    model.addAttribute(new HandleForm()); 
    return "/admin/upload"; 
} 

@RequestMapping(value = "/admin/upload-songs", method = RequestMethod.POST) 
public String doForm(@ModelAttribute(value = "handleForm") HandleForm handleForm, BindingResult result, Model model){ 

    if(result.hasErrors()){ 
     String stringList = null; 
     List<FieldError> errors = result.getFieldErrors(); 
     for (FieldError error : errors) { 
      stringList += error.getObjectName() + " - " + error.getDefaultMessage() + "\n"; 
     } 
     model.addAttribute("Response", stringList); 
     //model.addAttribute("songname", handleForm.getSongName()); works fine 
     //model.addAttribute("filename", handleForm.getFile().getOriginalFilename()); throws an error..? 
    }  

    return "/admin/upload"; 
} 

} 

Qualsiasi aiuto sarebbe molto apprezzato. Grazie in anticipo

risposta

17

è necessario aggiungere l'enctype alla dichiarazione del modulo?

enctype="multipart/form-data" 
4

assicurarsi di avere il risolutore per il file in più parti nel servlet dispatcher anche

<bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

    <!-- one of the properties available; the maximum file size in bytes --> 
    <property name="maxUploadSize" value="100000"/> 
</bean> 
Problemi correlati