TM.
TM.

Reputation: 111127

Struts - Uploading Files

I'm having a problem uploading a file using spring webflow 1.0 and struts 1.3.

The jsp is something like this:

<html:form action="/flowAction" method="post" enctype="multipart/form-data">
    <!-- snip -->
    <html:file property="file" name="attachDocumentsForm" size="50"/>
    <!-- snip -->
</html:form>

The Form is something like this:

public class AttachDocumentsForm extends SpringBindingActionForm {
    // note, SpringBindingActionForm extends struts' ActionForm
    private FormFile file;
    //snip
}

Now, my problem is that when I submit the form, the file field is always null. The other fields on the form are filled out properly, and if I dig through the RequestContext, I can find the file is buried deep some of the data structures there.

Here is the horribly ugly way that I can get at the attachment:

// 'context' is the RequestContext
ServletExternalContext servletExternalContext = (ServletExternalContext) context.getExternalContext();
ActionForm form = (ActionForm) servletExternalContext.getRequest().getAttribute("actionForm");
FormFile file = (FormFile) form.getMultipartRequestHandler().getFileElements().get("file");

I've noticed that the MultipartRequestHandler on my form is null, and I suspect that this might be part of the problem, but I've tried populating it with an instance of CommonsMultipartRequestHandler to no avail.

What do I need to do to let the file field be populated correctly?

Upvotes: 5

Views: 3333

Answers (2)

Naveen
Naveen

Reputation: 97

Add a field called file of type FormFile in your ActionForm (the one referred by attachDocumentsForm), along with the accessor methods.

The uploaded file can be accessed by calling form.getFile().

I hope this helps!

Upvotes: 0

Serge Bogatyrev
Serge Bogatyrev

Reputation: 816

I think you should configure spring dispatcher servlet: http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html#mvc-multipart-resolver

Upvotes: 1

Related Questions