2011-02-01 112 views
0

我發現從春季論壇上的代碼,這似乎是實現在Spring 3 wizardForms一種有趣的方式:WizardForm在春季3

@RequestMapping(method = RequestMethod.POST) 
public ModelAndView processSubmit(
@ModelAttribute("pet") Pet pet, 
SessionStatus status) { 
    if (pet.getFieldOne() == null) { 
     //return the form that will set field one's value 
     return new ModelAndView(...); 
    } else if (pet.getFieldTwo() == null) { 
     //return the form that will set field two's value 
     return new ModelAndView(...); 
    } //and so on for all the other field that need to be set... 
    ... 
    else { 
     //once the object has all necessary fields 
     //set and validated, then do what needs 
     //to be done to finish. Store object, end 
     //session, and return your success view. 
     this.clinic.storePet(pet); 
     status.setComplete(); 
     return new ModelAndView(...); 
    } 
} 

誰能告訴我什麼這裏的存儲單元中,這是一個很好的辦法?

回答

0

如果通過「存儲」您的意思是this.clinic.storePet(pet);,這是在嚮導完成時將完整對象保存在數據庫中的操作,以便它與嚮導實現完全無關。

該方法本身是在Spring 3中實現嚮導窗體的標準方式,它取代了已刪除的AbstractWizardFormController

請注意,它也需要@SessionAttribute("pet")作爲類級別的註釋。這個註解使得Spring可以在請求之間的會話中存儲相應的模型屬性,以便每個表單提交設置相同對象的字段。當所有字段被設置並且嚮導完成時,對象被保存到數據庫,並且被status.setComplete();從會話中移除。

+0

但是,如果我爲第一個表單頁面創建模型屬性對象,它如何爲空? – mjgirl 2011-02-01 09:59:37