Spring 控制器获取空对象
Spring controller get empty object
我正在通过 jsp 表单向 spring 控制器发送一个对象。
JSP:
<form:form modelAttribute="uploadItem" action="/uploadObject" method="post" enctype="multipart/form-data">
<form:input path="fileData" accept="audio/mpeg" type="file" id="file-upload" name="file-upload" />
<form:input type="text" path="title" id="upload-title" name="upload-title"/>
<input type="image" src="..." alt="Upload"/>
</form:form>
模型服务:
public void fillUploadMelodyModel(Model model) {
fillAdminRootModel(model);
model.addAttribute("uploadItem", new UploadedItem());
}
上传的项目:
public class UploadedItem {
private CommonsMultipartFile fileData;
private String title;
}
控制器:
@RequestMapping(value = "/uploadObject", method = RequestMethod.POST)
public String doUpload(UploadedItem uploadItem, BindingResult result, Principal principal) {
//at this point I get an empty object (null null values)
}
问题是什么?如何在 jsp?
中将对象传递给控制器
尝试像这样改变你的控制器
@RequestMapping(value = "/uploadObject", method = RequestMethod.POST)
public String doUpload(UploadedItem uploadItem,
BindingResult result,
@RequestParam("fileData") MultipartFile file,
@RequestParam("title") String title,
Principal principal) {
//Here you should receive your parameters
}
我认为您为文件 (file-upload) 和标题 (upload-title) 使用的名称与您的域 object 属性名称不同步。在 Jsp 页面中将您的名称更改为文件数据和标题。
我正在通过 jsp 表单向 spring 控制器发送一个对象。
JSP:
<form:form modelAttribute="uploadItem" action="/uploadObject" method="post" enctype="multipart/form-data">
<form:input path="fileData" accept="audio/mpeg" type="file" id="file-upload" name="file-upload" />
<form:input type="text" path="title" id="upload-title" name="upload-title"/>
<input type="image" src="..." alt="Upload"/>
</form:form>
模型服务:
public void fillUploadMelodyModel(Model model) {
fillAdminRootModel(model);
model.addAttribute("uploadItem", new UploadedItem());
}
上传的项目:
public class UploadedItem {
private CommonsMultipartFile fileData;
private String title;
}
控制器:
@RequestMapping(value = "/uploadObject", method = RequestMethod.POST)
public String doUpload(UploadedItem uploadItem, BindingResult result, Principal principal) {
//at this point I get an empty object (null null values)
}
问题是什么?如何在 jsp?
中将对象传递给控制器尝试像这样改变你的控制器
@RequestMapping(value = "/uploadObject", method = RequestMethod.POST)
public String doUpload(UploadedItem uploadItem,
BindingResult result,
@RequestParam("fileData") MultipartFile file,
@RequestParam("title") String title,
Principal principal) {
//Here you should receive your parameters
}
我认为您为文件 (file-upload) 和标题 (upload-title) 使用的名称与您的域 object 属性名称不同步。在 Jsp 页面中将您的名称更改为文件数据和标题。