gpt4 book ai didi

spring - Spring 上传表格(可选)和可选文件

转载 作者:行者123 更新时间:2023-12-04 13:52:03 25 4
gpt4 key购买 nike

我们正在使用配置文件创建一个配置文件页面,该表单上可以选择包含配置文件图片。我们正在使用Spring 3.2

形式如下:-

<form:form id="editMember" modelAttribute="memberAjaxEditModel"
method="POST" class="form-horizontal" enctype="multipart/form-data" >
...
<form:input path="fileData" type="file"/>
...
</form>

这是 Controller 方法:-
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String onEditPost(@PathVariable long id, @Valid @ModelAttribute(MemberAjaxEditModel.KEY) MemberAjaxEditModel model, BindingResult result) throws ServiceRecoverableException {
....
}

这是模型
public class MemberAjaxEditModel {

...
private CommonsMultipartFile fileData;
...
}

如果在表单上提交文件,则工作正常,但如果在提交表单时不带文件,则BindingResult变量中有错误。

错误如下:-
Field error in object 'memberAjaxEditModel' on field 'fileData': rejected value []; codes [typeMismatch.memberAjaxEditModel.fileData,typeMismatch.fileData,typeMismatch.org.springframework.web.multipart.commons.CommonsMultipartFile,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [memberAjaxEditModel.fileData,fileData]; arguments []; default message [fileData]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'fileData'; 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 'fileData': no matching editors or conversion strategy found]

最佳答案

原来是jQuery Form plugin发送了一个空字符串,而不是spring期望的-没有任何发送。

如果未像这样填充fileData值,我使用了一个before提交解决了该问题:-

 function beforeSubmit(arr, $form, options){
var fileDataIndex = -1;

$.each(arr, function(index, value) {
if (value.name == "fileData"){
if (value.value.length == 0){
fileDataIndex = index;
}
}
});

if (fileDataIndex != -1){
arr.remove(fileDataIndex);
}
}

希望这对一些有同样问题的Google员工有所帮助。

关于spring - Spring 上传表格(可选)和可选文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14771446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com