Spring 3 form binding object is always zero

I am trying to associate a nested object with Spring 3 and I am having problems.

JSP:

<portlet:actionURL var="formAction" />
<form:form id="add-objects-form" method="post" action="${formAction}">
   <input name = "obj.a"...>
   <input name = "obj.b"...>
   <input type = "file" multiple="multiple" name="file"/>
</form>

Form object

class FormObject{
private final static Logger logger = ...

private MultipartFile file
private Obj obj

...getters and setters
}

Controller:

@RequestMapping(method = RequestMethod.POST)
public void uploadDocument(@ModelAttribute FormObject formObject, BindingResult results ) {

}

formObjectgets obj.aand obj.b, but filealways null.

+3
source share
1 answer

Add modelAttribute="formObject"to<form:form>

Also make sure that you do not exclude debugging information from the classes. If you have or are not sure, indicate@ModelAttribute("formObject")

To process files (multipart data) you need to specify enctype for the form:

enctype="multipart/form-data"

Update: since you are using the js library to upload files, here is what to do:

  • Download only the image using an ajax request (don’t send anything). Store downloaded files in a temporary place.
  • .
  • ( ajax, submit), ,
  • .
  • , , , / .
+2

Source: https://habr.com/ru/post/1794566/


All Articles