Question

I'm struggling with the file upload on form:update with spring roo.

For the creation part, I used a form:multi tag provided by Jose Delgado here. The custom form:multi tag adds the enctype="multipart/form-data" to the form and that works fine.

The problem is when you want to offer the file upload capabilities to your update form. Spring Roo (maybe it's spring mvc, i don't know) will, by default, set enctype="application/x-www-form-urlencoded" to the update form (form:update tag). If I set the enctype attribute to enctype="multipart/form-data" in the upload form, the server will execute "create" method of the controller instead of the "udpate" method when the form is submitted...

Any idea how we could (simply) work around that? I spent quite some time on it already and I'm finding myself out of inspiration (maybe it's because it's the end of the day, also :).

Thanks for your help,

Kind Regards

Was it helpful?

Solution

OK...It seems that there's a little problem with the RequestMapping.

For whatever reason, the method parameters is set to "POST" when the multipart attribute is set to "true" in the form:update tag.

As a workaround, I check the _method parameter at the beginning of the create method. If it's set to "PUT", I return the value of the update method.

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid ActionRequest actionRequest, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {

        // Work around dispatcher bug: if the multipart attribute of the form is set to true,
        // submission of the update form routes to create method
        String toto = httpServletRequest.getParameter("_method");

        if(httpServletRequest.getParameter("_method").equals("PUT")){
            return this.update(actionRequest,bindingResult,uiModel,httpServletRequest);
        }
   ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top