简体   繁体   中英

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. The array I want to save is 1600 long (or 1599 if you start counting from 0). The index.index starts at 0 and the final number is 1599 (as it should be). 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.

Here is my code for the 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:

@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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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