【问题标题】:spring binding of list of list of objetcs对象列表列表的弹簧绑定
【发布时间】:2011-07-25 06:21:07
【问题描述】:

我有这个结构的对象

公共类清理{

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 中,我得到了清理对象的列表

谁能告诉我如何绑定这个列表并在控制器中获取提交的值

【问题讨论】:

    标签: spring data-binding


    【解决方案1】:

    在 jsp 中绑定并不难。如果您使用的是核心 jstl 标记(您应该这样做),您只需遍历列表条目并随心所欲地处理它们:

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

    既然你说的是“提交”,我猜你#re 试图在这里设置一个表单,这样会更容易(在此处使用 spring 的表单标签)&lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %&gt;

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

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

    @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
            }
    
        });
    }
    

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

    【讨论】:

    • 如果您不进行数据库调用,并且将其发布到控制器,您将如何处理?您的 setAsText 函数不处理列表项。你能告诉我你将如何处理物品清单吗?你可以在这里看到我的问题stackoverflow.com/questions/15730760/…
    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 2016-12-21
    • 1970-01-01
    • 2011-06-13
    • 2011-01-26
    • 2014-01-15
    • 2016-02-17
    • 2016-01-30
    相关资源
    最近更新 更多