简体   繁体   English

Thymeleaf 动态数组保存

[英]Thymeleaf dynamic array save

I'm trying to save a dynamic matrix as a string array in thymeleaf, while the displaying of the array works fine, the saving throws an error.我正在尝试将动态矩阵保存为 thymeleaf 中的字符串数组,虽然数组的显示工作正常,但保存会引发错误。 The array I want to save is 1600 long (or 1599 if you start counting from 0).我要保存的数组长度为 1600(如果从 0 开始计数,则为 1599)。 The index.index starts at 0 and the final number is 1599 (as it should be). index.index从 0 开始,最终数字是 1599(应该是)。 Not sure what the issue is, so any help would be appreciated.不知道是什么问题,所以任何帮助将不胜感激。 And one more thing, I found out, that if I used a number below 256 it seems to generally work, but the problem is, that I need the matrix to be able to work with numbers up to 4000 so using lower numbers isn't an option sadly.还有一件事,我发现,如果我使用低于 256 的数字,它似乎通常可以工作,但问题是,我需要矩阵能够处理高达 4000 的数字,所以使用较低的数字不是可悲的是一个选择。

Here is my code for the html:这是我的 html 代码:

 <form th:object="${projectData}" method="post" class="card">
    <div class="card-content">
        <div class="content">
            <div class="columns is-gapless is-multiline is-mobile">
                <tr th:each="_, index: ${projectData.matrixData}">
                    <div th:if="${index.index % matrixSize == 0}" class="break column is-desktop">
                    </div>

                    <input th:field="*{matrixData[__${index.index}__]}"
                           class="column"
                           type="text"
                           th:if="${#arrays.contains(matrixDisabledFields, index.index)}"
                           th:id="${index.index}"
                           disabled>

                    <input th:field="*{matrixData[__${index.index}__]}"
                           class="column"
                           type="text"
                           th:unless="${#arrays.contains(matrixDisabledFields, index.index)}"
                           th:id="${index.index}">
                </tr>
            </div>
            <input type="submit" class="button is-light is-pulled-right" value="Save">
        </div>
    </div>
</form>

And here is my code for the controller:这是我的 controller 代码:

@GetMapping("/matrix/{id}")
String getMatrix(Model model, @PathVariable Integer id) {
    ProjectData projectData = JsonConverter.getProjectDataWithID(id);

    if (projectData == null) {
        model.addAttribute("error", "Invalid id");
        return "error";
    }

    String[] matrix;
    if (projectData.getMatrixData() == null) {
        matrix = new String[(int) 
        Math.pow(projectData.getProjectOptions().getMatrixSize(),2)];
    } else {
        matrix = projectData.getMatrixData();
    }

    int matrixLength = (int) Math.sqrt(matrix.length);

    Integer[] matrixDisabledFieldIds = new Integer[matrixLength];

    Integer pastIndex = -matrixLength - 1;
    for (int i = 0; i < matrixLength; i++) {
        matrixDisabledFieldIds[i] = pastIndex + matrixLength + 1;
        pastIndex = matrixDisabledFieldIds[i];
        matrix[pastIndex] = "X";
    }

    projectData = new ProjectData();
    projectData.setMatrixData(matrix);

    model.addAttribute("matrixSize", matrixLength);
    model.addAttribute("matrixDisabledFields", matrixDisabledFieldIds);
    model.addAttribute("projectData", projectData);

    return "matrix";
}

The error that is thrown on saving is this one:保存时抛出的错误是这个:

2022-08-27 12:13:56.630 ERROR 21000 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/]. 
[dispatcherServlet]    : 
Servlet.service() for servlet [dispatcherServlet] in context with 
path [] threw exception [Request processing failed; nested exception is 
org.springframework.beans.InvalidPropertyException: Invalid property 'matrixData[1000]' of 
bean class [com.tribus.tribusspring.entities.objects.project.ProjectData]: Invalid array index 
in property path 'matrixData[1000]'; nested exception is 
java.lang.ArrayIndexOutOfBoundsException] with root cause

java.lang.ArrayIndexOutOfBoundsException: null
at java.base/java.lang.reflect.Array.set(Native Method) ~[na:na]
at 
org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty 
(AbstractNestablePr 
opertyAccessor.java:313) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue 
(AbstractNestableProper 
tyAccessor.java:275) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue 
(AbstractNestablePropertyAccessor.java:266) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues 
(AbstractPropertyAccessor.java:104) ~[spring-beans-5.3.22.jar:5.3.22]
at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:889) ~ 
[spring-context-5.3.22.jar:5.3.22]
at org.springframework.validation.DataBinder.doBind(DataBinder.java:780) ~[spring-context- 
5.3.22.jar:5.3.22]
at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:207) ~[spring-web- 
5.3.22.jar:5.3.22]

Spring MVC limits the number of parameters processed in the DataBinder to 256. See Javadoc of org/springframework/validation/DataBinder.java. Spring MVC 将 DataBinder 中处理的参数数量限制为 256 个。请参阅 org/springframework/validation/DataBinder.java 的 Javadoc。 That may cause your exception (I didn't tried it out).这可能会导致您的异常(我没有尝试过)。 Give it a try to configure your data binder with a higher value for autoGrowCollectionLimit as shown for example in this article: https://dzone.com/articles/spring-initbinder-for-handling-large-list-of-java尝试使用更高的 autoGrowCollectionLimit 值配置数据绑定器,如本文所示: https://dzone.com/articles/spring-initbinder-for-handling-large-list-of-java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM