【问题标题】:How to submit two dimentional array in Thymeleaf?如何在 Thymeleaf 中提交二维数组?
【发布时间】:2019-11-27 21:49:51
【问题描述】:

我正在尝试提交包含二维数组的表单。你能检查我的代码并告诉我它有什么问题吗?

表格:

public class SimpleForm {

    private String[][] twoDimentionArray;

    public String[][] getTwoDimentionArray() {
        return twoDimentionArray;
    }

    public void setTwoDimentionArray(String[][] twoDimentionArray) {
        this.twoDimentionArray = twoDimentionArray;
    }
}

百里香:

<form action="#" th:action="@{/save-form} th:object="${form}" method="post"> 

    <input type="text" th:field="*{twoDimentionArray[0][0]}"/>

    <input type="submit" value="Submit" />

</form>

出现以下错误:

java.lang.IllegalArgumentException: array element type mismatch
    at java.lang.reflect.Array.set(Native Method) ~[na:1.8.0_171]
    at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:311) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:275) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:266) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:839) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:735) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:197) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:107) ~[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:157) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]

【问题讨论】:

    标签: java spring-boot thymeleaf submit


    【解决方案1】:

    您传递的数组错误。试试这个

    <form action="#" th:action="@{/save-form}" th:object="${form}" method="post"> 
        <div th:each="1d, i: *{twoDimentionArray}">
            <div th:each="test, j: *{1d}">
                <input type="text" th:field="*{userAddresses[__${i.index}__][__${j.index}__]}" />
           </div>
        </div>
    
        <input type="submit" value="Submit" />
    
    </form>
    

    【讨论】:

    • 感谢您的回答。我的示例中的数组已经初始化并填充到控制器中。更何况一维数组即使没有初始化也没问题。
    • @polishgrand 如果我解决了您的问题,请不要忘记将其标记为正确答案 :)
    • 很遗憾问题依然存在,数组的初始化不是问题的解决方案
    • 你为什么不把它作为一个列表列表?
    • 因为在我的情况下,功能与索引严格相关
    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多