【问题标题】:Sending a list of objects from view to controller : limited to 256 objects将对象列表从视图发送到控制器:限制为 256 个对象
【发布时间】:2014-12-02 15:27:31
【问题描述】:

我的视图中有一个表单,可以将对象发送到我的控制器,但问题是,如果我发送的对象超过 256 个,则会出现异常:

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

所以我想知道为什么会有这样的限制,我发现了这个话题:https://stackoverflow.com/a/24699008/4173394

但它似乎对我不起作用(可能对我来说不好用)。

这是我的结构: 我的视图称为 createUpdate.vm 并像这样发布我的表单:

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

我在 FollowerFileController 中的函数 upsertFollowerFile :

    @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) {

还有我的班级 FollowerFileDto :

public class FollowerFileDto {

    private String title;

    private Long followerId;

    private boolean isDeletable;

    private List<FollowerDto> followers;

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

正如您在我的控制器中看到的,我尝试使用 @InitBinder 注释设置超过 256 个允许的对象 (500),但它根本不起作用。 InitBinder 函数永远不会被调用。我做错什么了吗? 谢谢你的回答;)

【问题讨论】:

  • 您可以尝试不带 Secured 注释的 initBinder。不应该是 upserFollowerFile 吗?
  • 我尝试评论@Secured,但我遇到了同样的问题。
  • 你真的需要 multipart/form-data 吗?
  • 可能不会。我尝试不使用 multipart/form-data,它的工作原理相同:小于 256 可以,超过 257 的堆栈跟踪相同。
  • 您能否在最初的问题中指定您正在使用的 RequestMapping 方法?

标签: java spring spring-mvc jakarta-ee post


【解决方案1】:

实际上,@InitBinder 没有被读取,这就是为什么没有设置新的收集限制。我不得不将我的 springmvc-router 版本升级到 1.2.0(这也迫使我将我的 spring 版本升级到 3.2)。

在这些升级之后,使用相同的代码,它可以工作;)

【讨论】:

    【解决方案2】:

    Spring 只允许列表中从 &lt;form action="..."&gt;@Controller 的 255 个对象以避免 OutOfMemory 问题。要增加此限制,请在 initBinder() 方法中添加 binder.setAutoGrowCollectionLimit(1000)WebDataBinder 是一个将请求参数绑定到 JavaBean 对象的 DataBinder。 initBinder() 方法必须放在 ControllerController 的父级中。

    @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));
    }
    

    【讨论】:

    • 就在现场。谢谢你的回答。
    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多