2016-10-09 29 views
0

我有以下形式的模型:Java的春天FormFactory.form嵌套对象

ReservationLockRequestForm:

public class ReservationLockRequestForm { 
    private Restaurant restaurant; 
    private ReservationInquiryResponse reservationData; 
    private Time reservationTime; 
} 

我离开了干将,setter和易读性空构造函数。

现在,如果我把这个

formFactory.form(ReservationLockRequestForm.class).bindFromRequest().get() 

我得到

Invalid property 'restaurant[tables][1][numberOfChairs]' of bean class [models.helpers.forms.ReservationLockRequestForm]: Illegal attempt to get property 'restaurant' threw exception 

Restaurant模型包含一个List<Tables>对象,而Tables模型确实含有numberOfChairs属性。

我在做什么错?

编辑:添加一个断点ReservationLockRequestFormRestataurant Setter revels传入的Restaurant对象为空(所有属性都为空),但快速检查它包含所有数据的请求revels。

回答

0

好吧我修好了。原来前端将请求作为Form-Data发送,而不是JSON。我改变了这一点,并没有任何修改后端它完美地工作

0

您需要定制属性编辑器。你需要有一个课程,例如PropertyEditorSupport,例如RestaurantPropertyEditor extends PropertyEditorSupport,负责将餐馆或任何字段转换为文本表示。它将需要覆盖setAsTextgetAsText

然后在你的控制器返回的视图,你需要有

@InitBinder("reservationLockRequestForm ") 
public void initBinder(WebDataBinder binder) 
{ 
    binder.registerCustomEditor(Restaurant .class, new RestaurantPropertyEditor()); 
    // ... other ones too 
} 

通常你PropertyEditor需要你的DAO从一个ID转换成实体,反之亦然,你需要做的这一切都是你自己

有时候不直接使用Spring Binding与实体并且手动处理post/get中的请求参数更容易。记住这一点,我发现这是处理参数集合的情况。