Andry
Andry

Reputation: 665

Spring MVC Controller List<MyClass> as a parameter

I have a Spring MVC application. Jsp page contains form, which submitting. My controller's method looks like:

@RequestMapping(value = "photos/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public String fileDetailsUpload(List<PhotoDTO> photoDTO,
                                    BindingResult bindingResult,
                                   HttpServletRequest request) {
        // in the point **photoDTO** is null
    }

My class is:

    public class PhotoDTO {
        private Boolean mainPhoto;
        private String title;
        private String description;
        private Long realtyId;

//getters/setters

But, if I write the same method, but param is just my object:

    @RequestMapping(value = "photos/fileUpload", method = RequestMethod.POST)
        @ResponseBody
        public String fileDetailsUpload(PhotoDTO photoDTO,
                                        BindingResult bindingResult,
                                        HttpServletRequest request) {
// Everething is Ok **photoDTO** has all fields, which were fielded on the JSP

What should I do in the situation? What if I have on Jsp many PhotoDTO objects (15) how can I recive all of them on the server part?

Upvotes: 1

Views: 3849

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

The page should pass the parameters for each PhotoDTO in the list back to the server in the appropriate format. In this case you need to use the status var to specify the index of each object in the list using the name attribute.

<form>
<c:forEach items="${photoDTO}" var="photo" varStatus="status">
        <input name="photoDTO[${status.index}].title" value="${photo.title}"/>
        <input name="photoDTO[${status.index}].description" value="${photo.description}"/>
</c:forEach>
</form>

Upvotes: 2

Related Questions