Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a list of objects from view to controller : limited to 256 objects

I've got a form in my view which send objects to my controller, but the matter is that I've got an exception if I send more than 256 objects :

org.springframework.beans.InvalidPropertyException: Invalid property 'followers[256]' of bean class [org.myec3.portalgen.plugins.newsletter.dto.FollowerFileDto]: Index of out of bounds in property path 'followers[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256

So I was wondering why such a limit, and I found this topic : https://stackoverflow.com/a/24699008/4173394

But it doesnt seem to work for me (probably a bad use from me).

Here is my structure : My view is called createUpdate.vm and post my form like this :

<form id="createFollowerFileForm" method="post" action="#route("followerFileController.upsertFollowerFile")" enctype="multipart/form-data" class="form_styled">

My function upsertFollowerFile in FollowerFileController :

    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        // this will allow 500 size of array.
        dataBinder.setAutoGrowCollectionLimit(500);
    }

    @Secured({ "ROLE_SUPER_ADMIN_PORTALGEN", "ROLE_CUSTOMER_PORTALGEN", "ROLE_ADMIN_PORTALGEN", "ROLE_WRITER_PORTALGEN" })
    public String upsertFollowerFile(
            @ModelAttribute(value = "followerFile") FollowerFileDto followerFileDto,
            BindingResult result, ModelMap model, HttpServletRequest request) {

And my class FollowerFileDto :

public class FollowerFileDto {

    private String title;

    private Long followerId;

    private boolean isDeletable;

    private List<FollowerDto> followers;

    public FollowerFileDto() {
        this.followers = new ArrayList<FollowerDto>();
    }

As you can see in my controller, I tried to set more than 256 allowed objects (500) with the @InitBinder annotation, but it doesnt work at all. The InitBinder function is never called. Did I do anything wrong ? Thanks for you answers ;)

like image 578
Logan Paul Avatar asked Dec 02 '14 15:12

Logan Paul


2 Answers

Actually, the @InitBinder was not read, that's why new collection limit was not set. I had to upgrade my springmvc-router version to 1.2.0 (which also forced me upgrade my spring version to 3.2).

After those upgrades, with the same code, it works ;)

like image 176
Logan Paul Avatar answered Nov 02 '22 21:11

Logan Paul


Spring allow only 255 Objects in a list from <form action="..."> to @Controller to avoid OutOfMemory problem. To increase this limit add binder.setAutoGrowCollectionLimit(1000) in initBinder() method. WebDataBinder is a DataBinder that binds request parameter to JavaBean objects. The initBinder() method must be put in Controller or parent of the Controller.

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.setAutoGrowCollectionLimit(1000);

    // SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    // dateFormat.setLenient(false);
    // binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
like image 36
Mr. Mak Avatar answered Nov 02 '22 19:11

Mr. Mak