views:

2359

answers:

6

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.

+1  A: 

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 usual application/x-www-form-urlencoded.

When you specify multipart/form-data, then in Spring you need to use a MultipartResolver 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.

skaffman
Thanks for the reply. I'll try that and report back if it worked.
er4z0r
How would I do this with my existing entity?I've managed to get the example from the mvc-docs working for me. But the problematic part is: now that I have the byte[] in my controller method, I need to do something with it ;-) E.g. I have a DataFile entity and now I want to bind the byte[] that was uploaded via my FileUploadController to such an entity and sotre that in the database. How would I do that?
er4z0r
+1  A: 
er4z0r
A: 

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.

Mark
I haven't looked into Converters/Formatters yet, but so far registering the CustomEditor seems quite easy to me. Note: I only had to register it. I didn't have the "joy" to write a custom property-editor ;-)
er4z0r
+1  A: 

See https://jira.springsource.org/browse/ROO-442 for the related Roo issue.

Ben Alex
Already voted on that one. And btw: Nice to see you round here, Ben ;-)
er4z0r
A: 

I also have the same problem as you. I will try to solve it with your problem but i still not clear that in the view, what do you choose to use in your code? input type="file" or spring form tag?

Because when i use input tag, spring roo can not pass the model. It shows the error like " Failed to invoke handler method [public void egat.spring.roo.ptu.io.web.ImportExcelController.post(java.lang.Long,org.springframework.ui.ModelMap,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping "

Could you please explain to me ?

Thank you very much.

Moddy
Sorry, I don't have access to the jsp-code right now, but make sure that you:- have registered multipartresolver in your context (see Spring docs)- set the correct enctype on the form-tag in your jsp (should be "multipart/form-data"- also check the name and path attributes of the input fields you use in the jspMy upload uses a standard input tag of the type file. Make sure the name attribute of your file input is the same as the byte[] property in your entity.
er4z0r
A: 

I have a simple blog entry on how to handle Spring Multipart Form Upload, which might be helpful.

ritesh adval