[英]Spring mvc handling differences in types between form and model
我有这个表格
<form:form action="saveCustomer" modelAttribute="customer" enctype="multipart/form-data" method="POST">
<!-- need to associate this data with customer id -->
<form:hidden path="id" />
<table>
<tbody>
<tr>
<td><label>First name:</label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><label>Last name:</label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td><label>Email:</label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><label>Profile Image:</label></td>
<td>
<form:input type="file" path="file" id="file" class="form-control input-sm"/>
</td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" value="Save" class="save" /></td>
</tr>
</tbody>
</table>
</form:form>
和这个模型
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
@NotEmpty
@Column(name="file")
private String file;
}
在我的模型中,我决定不将文件字段定义为MultipartFile
,而是使用String
。
我这样做是为了让我能够获取已上传文件的文件名,并留下spring mvc来上传文件。 那行得通,但是当我引入错误检查时,出现此错误:
org.springframework.validation.BeanPropertyBindingResult:1个错误字段'file'中的对象'customer'中的字段错误:拒绝的值[org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@2a8400bb]; 代码[typeMismatch.customer.file,typeMismatch.file,typeMismatch.java.lang.String,typeMismatch]; 参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[customer.file,file]; 参数[]; 默认消息[文件]]; 默认消息[未能将类型'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest $ StandardMultipartFile'的属性值转换为属性'file'的必需类型[java.lang.String]; 嵌套的异常是java.lang.IllegalStateException:无法将属性“文件”的类型[org.springframework.web.multipart.support.StandardMultipartHttpServletRequest $ StandardMultipartFile]的值转换为所需类型[java.lang.String]:没有匹配的编辑器或转换找到策略]
我的控制器:
@RequestMapping(value = "/saveCustomer", method = RequestMethod.POST)
public String saveCustomer(@Valid FileBucket fileBucket,
ModelMap model, @ModelAttribute("customer") Customer theCustomer,BindingResult result) throws IOException {
if (result.hasErrors()) {
System.out.println("validation errors");
return "customer-form";
} else {
System.out.println("Fetching file");
MultipartFile multipartFile = fileBucket.getFile();
// Now do something with file...
FileCopyUtils.copy(fileBucket.getFile().getBytes(), new File( UPLOAD_LOCATION + fileBucket.getFile().getOriginalFilename()));
String fileName = multipartFile.getOriginalFilename();
model.addAttribute("fileName", fileName);
theCustomer.setFile(fileName);
customerService.saveCustomer(theCustomer);
return "redirect:/customer/list";
}
}
如何处理此错误?
在您的错误部分中只说一下
typeMismatch.customer.file,typeMismatch.file,typeMismatch.java.lang.String,typeMismatch
但是在实体类文件中是一个字符串
<form:form action="saveCustomer" modelAttribute="customer" enctype="multipart/form-data" method="POST">
以您的形式
<form:input type="file" path="file" id="file" class="form-control input-sm"/>
但是,这里的modelAttribute是客户前端,它是文件和文件输入,后端是String
因此您遇到了问题
private String file
是不正确的参数,因为它是一个多部分数据,因此您应该在实体类中使用MultipartFile
@Entity
@Table(name="customer")
public class Customer {
... . . . . ..
private MultipartFile file;
//getters setters
}
我创建了另一个名为path
字段并使file
瞬态
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
@NotEmpty
@Column(name="path")
private String path;
@Transient
private MultipartFile file;
并且有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.