2011-02-01 111 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