I want to upload a file to my spring 3.0 applicatoin (created with roo).
I already have the following entity:
@Entity
@RooJavaBean
@RooToString
@RooEntity
public class SelniumFile {
@ManyToOne(targetEntity = ShowCase.class)
@JoinColumn
private ShowCase showcase;
@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] file;
@NotNull
private String name;
}
But I am not sure how to implement it on the view/controller side. Can I freely mix spring-form tags like <form:input>
with normal tags like <input type=file ...>
?
I have seen the nice multipart upload section in the MVC-Documentation but still need a little help to apply it to my specific case.
If you are using Spring 3.0 then you can create a Converter and a Formatter(optional) And you won't have to use the initBinder method, and keep things more POJO, but your solution is still very valid and still quite clean.
I don't believe you can mix-and-match file uploads with normal forms (in Spring MVC, at least), because file upload forms use the
multipart/form-data
encoding, rather than the usualapplication/x-www-form-urlencoded
.When you specify
multipart/form-data
, then in Spring you need to use aMultipartResolver
implementation (as mentioned in the Spring docs you linked to), and all parameter decoding must go through that. Spring MVC will not be able to decode the normal form inputs, since all fields will be encoded along with the uploaded file.It's almost certainly easier to use two separate forms, one for the normal stuff, one for the file upload.
See https://jira.springsource.org/browse/ROO-442 for the related Roo issue.
You need to have two forms, one to upload file and other to upload data. I think about this because a file is big archive or a litle and because this situation this do not work in a easy way.
Update: I think my question was badly formulated. What I wanted to do is create a spring
I found a very good explanation on how to do it in the older spring documentation and applied it to the new Spring 3.0 MVC. Basically this means you need to register a PropertyEditor in your controllers @InitBinder method. Afterwards everything will behave as expected (provided you have added MultiPartResolver to the context and set the correct form encoding). Here is my sample: