Henrique Ordine
Henrique Ordine

Reputation: 3337

Spring form with only one input of type=file

I'm trying to upload a MultipartFile with spring.

I want to used the Spring form tags to contain the input of type file which will eventually upload this file to my controller. So I created a FileForm class which is just a java bean with only one property on the type MultipartFile.

My JSP looks like this:

        <form:form modelAttribute="azrAgbFileForm" action="${uploadAzrAgb}" method="POST" enctype="multipart/form-data" cssClass="form-horizontal highlighted" role="form">
                <label for="uploadFile_upload_azrAgb" class="col-sm-4 control-label"><img src="<c:url value="/public/resources/images/doc.png"/>"></label>
                <form:input id="uploadFile_upload_azrAgb" path="file" type="file" cssClass="form-control" cssErrorClass="form-control error"/>
                <button type="submit" class="btn btn-primary" style="float:left; margin-top: 10px;">Upload bestand</button>
        </form:form>

And my controller method like this:

@RequestMapping(value="/azrAgb", method={RequestMethod.GET, RequestMethod.POST})
public String uploadAzrAgb(@ModelAttribute(value="azrAgbFileForm") FileForm form, BindingResult result, Model model) {
    //doing some stuff here
}

The problem is that when I click on submit, my model attribute azrAgbFileForm is present in my controller method, by its property file is null, even though I did select a file to upload, and my input's path is set to "file", in my JSP.

I've also configured my multipartBean like this:

<beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</beans:bean>

Why is my form's file property not being set and do I really need a form class for this? Is there a way to use Spring's Form tag without in this case creating this Form class with only one property?

As requested, my web.xml starts like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <!-- some servlet and security configurations -->
</web-app>

Upvotes: 1

Views: 4543

Answers (1)

Henrique Ordine
Henrique Ordine

Reputation: 3337

I added this little tag to my DispatcherServlet configuration in my web.xml and it worked:

<multipart-config />

Upvotes: 3

Related Questions