Datei-Upload in Spring 3 MVC - Null-Zeiger-Ausnahme

Bin ich mit spring MVC 3. Ich habe versucht, Zugriff auf die Attribute der hochgeladenen Datei, aber ich bekomme immer folgende Fehlermeldung. Ich kann auf die anderen Felder des Formulars, die ist gebucht, aber ich kann keinen Zugriff auf die hochgeladene Datei.

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 - /JSP-Datei

<%@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>

Form 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;
}  

}

Den 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";
}

}

Jede Hilfe wäre sehr geschätzt werden. Vielen Dank im Voraus

InformationsquelleAutor user866190 | 2012-02-13
Schreibe einen Kommentar