Po nějaké době praxe řešíte většinu problému rutinně, metodou kouknu a vidím. Občas je to ovšem zásek, se kterým na první pohled netušíte, jak pohnout. Tohle je jeden z takových. Měli jsme JSP formulář, který fungoval rok bez chyby. Renderoval se v pořádku, ale binding začal házet IndexOutOfBoundsException. Ale přitom na modelu byl field typu java.util.List Už máte řešení? Tak to asi znáte Spring jako své boty.

Řešení

Pes je zakopaný v DataBinder.html#setAutoGrowCollectionLimit(int) a jeho výchozím nastavení na 256 kvůli prevenci OutOfMemoryErrors.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
/**
* @author banterCZ
*/
@Controller
public class MyController {
@Value("${myApplication.autoGrowCollectionLimit:1000}")
private int autoGrowCollectionLimit;
/**
* {@link org.springframework.validation.DataBinder#DEFAULT_AUTO_GROW_COLLECTION_LIMIT} is only 256,
* but there are much more items.
* @param dataBinder
*/
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setAutoGrowCollectionLimit(autoGrowCollectionLimit);
}
//TODO
}