gpt4 book ai didi

java - Spring 3 对象与原始数据类型的绑定(bind)列表

转载 作者:行者123 更新时间:2023-12-01 13:31:12 26 4
gpt4 key购买 nike

我是初学者,正在使用 Spring 3。

我有以下 bean

public class Record {
private String title;
private double amount;

//Getter and Setter
}

public class RecordBean {
private List<Record> records;

public RecordBean() {
this.records = LazyList.lazyList(new ArrayList<Record>(), new InstantiateFactory<Record>(Record.class));
}

//Getter and Setter
}

我已添加RecordBeanmodel

modelMap.add('recordBean',new RecordBean());

以下是 Spring 形式

<form:form action="/save" method="POST" commandName="recordBean">

<form:input path="records[0].title"/>
<form:input path="records[0].amount"/>

<form:input path="records[1].title"/>
<form:input path="records[1].amount"/>

<input type="submit" value="Save"/>
</form:form>

我还添加了InitBinder到 Controller

@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));
}

现在,如果我输入 amount[0] 的值并将 amount[1] 留空并提交表格,我得到 typeMismatch错误。

Field error in object 'recordBean' on field 'records[1].amount': rejected value []; codes [typeMismatch.recordBean.records[1].amount,typeMismatch.recordBean.records.amount,typeMismatch.records[1].amount,typeMismatch.records.amount,typeMismatch.amount,typeMismatch.double,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [recordBean.records[1].amount,records[1].amount]; arguments []; default message [records[1].amount]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'double' for property 'records[1].amount'; nested exception is java.lang.NumberFormatException: empty String]

请指教。

谢谢

最佳答案

也许尝试将这一行从“double”更改为“Double”:

private double amount;

关于java - Spring 3 对象与原始数据类型的绑定(bind)列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21558215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com