在Spring MVC表单中绑定嵌套属性

时间:2018-01-16 07:26:43

标签: java spring spring-mvc data-binding

我在Spring mvc 4中绑定嵌套对象属性时遇到问题。 这是场景:

Form.java:

@Entity
@Table (name = "form")
public class Form implements java.io.Serializable{

@SuppressWarnings("unchecked")
private List<Field> fieldsList = LazyList.decorate(new ArrayList<Field>(),  
           FactoryUtils.instantiateFactory(Field.class));

Field.java

@Entity
@Table(name="field")
public class Field implements java.io.Serializable {

private FieldType fieldType;

FieldType.java

@Entity
@Table(name="field_type")
public class FieldType implements java.io.Serializable {

private Integer id;
private String name;
private List<Field> fields;

我发送一个Form对象,其中包含Field对象列表:

@RequestMapping(method = RequestMethod.GET)
public ModelAndView editForm(HttpServletRequest request){
    ModelAndView model = new ModelAndView("denf_dynamic_form");
    Form f = formBackingObject(request);
    model.addObject("command", f);
    model.addObject("origFormName",f.getFormName());
    return model;
}

在JSP中,我试图绑定form.fieldsList.fieldType.name属性,如下所示:

<c:forEach items="${command.fieldsList}" var="fi" varStatus="i">

<spring:bind path="command.fieldsList[${i.index}].fieldType.name"> 
    <input style="display:none;" id='fTypeName' name='${status.expression}' 
    value="${status.value }"/>
</spring:bind>

如果字段列表很小,绑定工作正常。随着字段数量变大(大约100),行为变得异常;与Form类直接相关的属性正确绑定。但是FieldType类的嵌套属性(如fieldType.name)无法绑定。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

只是更新: 原因是为fieldType对象生成了相同的id。

Basically they are referencing to same object. That is why spring bind is not working.

这些ID是否有任何独特之处?