Search code examples
javaspringmodel-view-controllermodelattribute

I want to submit HTML form into mysql DB using Java MVC framwork. I cannot access file from the HTML page


FORM:

<form action='/ticketDataCollector/' method='post'>
    <tr>
        <td>
            <label>Attachment</label>
        </td>
        <td>
            <input type='file' name='attachment'>
        </td>
    </tr>
...

ticketDataCollector method signature:

public ModelAndView ticketDataCollector(@ModelAttribute("ticketObject") Ticket ticketObject)

Ticket is java class:

private File attachment;

public File getAttachment(){
    return attachment
}

getAttachment() returns null when I select a file on the HTML page. Is there anything else that I need to do?


Solution

  • Configure MultiPartResolver and change content type to multipart/form-data in Your html form. See example here: https://spring.io/guides/gs/uploading-files/ And i think that spring will not autowire file in Your model. You need to do it manually..