spring对象列表列表的绑定

时间:2011-07-25 06:21:08

标签: spring data-binding

我有这个结构的对象

公共课堂清洁{

private Contacts contact;
private List<Contacts> dnb360;
private String operation;   

public String getOperation() {
    return operation;
}
public void setOperation(String operation) {
    this.operation = operation;
}
public Contacts getContact() {
    return contact;
}
public void setContact(Contacts contact) {
    this.contact = contact;
}
public List<Contacts> getDnb360() {
    return dnb360;
}
public void setDnb360(List<Contacts> dnb360) {
    this.dnb360 = dnb360;
}

在jsp中我得到了清理对象列表

任何人都可以告诉我如何绑定此列表并在控制器中获取提交的值

1 个答案:

答案 0 :(得分:1)

在jsp中绑定并不困难。如果你正在使用核心的jstl标签(你应该这样做),你只需要遍历列表条目并按照你想要的方式对它们进行迭代:

<c:forEach items="${dnb360}" var="s">
      <b>${s}</b> 
</c:forEach>

由于您正在谈论“提交”,我猜您正在尝试在此处设置表单,这样会更容易(在此处使用spring的表单标记)<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<form:checkboxes items="${dnb360}" path="dnb360" />

为了检索这样的绑定值并转换回您的对象,我建议使用@InitBinder注释。您使用该方法注释方法,并定义在通过具有@ModelAttribute("dnb360") dnb360,...

的方法检索此String时如何将String值绑定回对象
@InitBinder
public void initBinder(WebDataBinder binder) {
    //custom editor to bind Institution by name
    binder.registerCustomEditor(Contacts.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(xxx); //most likely obtained through database call
        }

    });
}

如果您需要更多帮助,请随时提出。