我有一个定义为:的控制器
public void saveCustomer(@Valid @ModelAttribute("customer") Customer customer) {
// persist
}
和客户:的吸气剂
public List<ContactInfo> getContactInfo() {
if(contactInfo != null) {
return contactInfo;
}
else {
return new ArrayList<ContactInfo>();
}
}
如果我用HTML表单点击控制器,那么联系人信息为空但是,如果我将getter更改为
public List<ContactInfo> getContactInfo() {
return contactInfo;
}
则联系人信息被正确地绑定和持久化。我真的很困惑为什么会发生这种事。
我已经在Chrome中进行了检查,联系信息肯定在请求参数中为:
contactInfo[0].alias:test
contactInfo[0].email:test@test.com
如果你不让contactInfo
变成非null
,它只会继续做一个新的ArrayList
:
public List<ContactInfo> getContactInfo() {
if(contactInfo == null) {
contactInfo = new ArrayList<ContactInfo>();
}
return contactInfo;
}