Forms 春季奇才3
我从Spring论坛中找到一段代码,这似乎是在Spring 3中实现wizardForms的一种有趣的方式:Forms 春季奇才3,forms,spring-mvc,wizard,Forms,Spring Mvc,Wizard,我从Spring论坛中找到一段代码,这似乎是在Spring 3中实现wizardForms的一种有趣的方式: @RequestMapping(method = RequestMethod.POST) public ModelAndView processSubmit( @ModelAttribute("pet") Pet pet, SessionStatus status) { if (pet.getFieldOne() == null) { //return the f
@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( ... );
}
}
有人能告诉我这里的存储是什么意思吗?这是一种好方法吗?如果“存储”是指this.clinic.storePet(pet)代码>,它是在向导完成时将完整对象保存到数据库中的操作,因此它与向导实现完全无关
这种方法本身是Spring3中实现向导表单的标准方法,它取代了不推荐的AbstractWizardFormController
注意,它还需要@SessionAttribute(“pet”)
作为类级注释。此注释使Spring在请求之间的会话中存储相应的模型属性,以便每个表单提交设置相同对象的字段。设置所有字段并完成向导后,对象将保存到数据库中,并通过status.setComplete()从会话中删除
但是如果我为第一个表单页面创建模型属性对象,它怎么会为null?@mjgirl:该对象不是null
。它的字段是空的,因为它们没有设置。真的吗?我的代码公共字符串onSubmit(@ModelAttribute(“giveForm”)giveForm giveForm,SessionStatus状态){if(giveForm.getKeyId()=null){return“give”;}else{System.out.println(giveForm.getKeyId());status.setComplete();return“give-r”;从未返回give-view,但仍然有一个问题:用这个按钮实现取消和上一个按钮容易吗?