Spring 3 中的 WizardForm
我从 Spring 论坛找到了一段代码,这似乎是在 Spring 3 中实现 WizardForms 的一种有趣的方式:
@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( ... );
}
}
谁能告诉我这里的存储意味着什么,这是一个好方法吗?
I found a code from Spring forums that seems to be an interesting way to implement wizardForms in Spring 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( ... );
}
}
Can anyone tell me what the storing here means, and is this a good way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“存储”指的是
this.clinic.storePet(pet);
,那么它是在向导完成时将完整对象保存到数据库中的操作,因此它与向导实现完全无关。该方法本身是在 Spring 3 中实现向导表单的标准方法,它取代了已弃用的
AbstractWizardFormController
。请注意,它还需要
@SessionAttribute("pet")
作为类级别注释。该注解使得Spring在请求之间的会话中存储相应的模型属性,以便每次表单提交都设置同一个对象的字段。设置完所有字段并完成向导后,对象将保存到数据库,并通过status.setComplete();
从会话中删除。If by "storing" you mean
this.clinic.storePet(pet);
, it's an action of saving the complete object in your database when wizard is finished, so that it's completely unrelated to wizard implementation.The approach itself is a standard way to implement wizard forms in Spring 3 that replaces the deprectated
AbstractWizardFormController
.Note that it also requires
@SessionAttribute("pet")
as a class-level annotation. This annotation makes Spring to store the corresponding model attribute in the session between requests, so that each form submission sets fields of the same object. When all fields are set and wizard is finished, object is saved to the database, and removed from the session bystatus.setComplete();
.