Spring 3 中的 WizardForm

发布于 2024-10-15 03:49:16 字数 935 浏览 6 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2024-10-22 03:49:16

如果“存储”指的是 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 by status.setComplete();.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文