我正在使用
spring MVC 3.我一直在尝试访问上传文件的属性,但我不断收到以下错误消息.我可以访问已发布的表单的其他字段但我无法访问上传的文件.
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文件
JSP Page${Response}
Upload Songs
Song Name : | |
Artist Name : | |
Gendre : | |
description : | |
Browse File : | |
表单处理程序类
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;
}
}
控制器
@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 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";
}
}
任何帮助将非常感激.提前致谢