AbstractWizardFormController使用Annotated @Controllers [英] AbstractWizardFormController using Annotated @Controllers

查看:144
本文介绍了AbstractWizardFormController使用Annotated @Controllers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring Framework中, AbstractWizardFormController 似乎已被弃用。如何在Spring MVC Framework中实现多个页面。 (我不使用webflow)

In Spring Framework , AbstractWizardFormController seems deprecated. How to implement multiple pages form in the Spring MVC Framework. (I am not using webflow)

任何示例或指针都有助于考虑我在Spring中的有限知识。

any example or pointer would help considering my limited knowledge in Spring.

推荐答案

@Controller是一种更灵活的方式来定义表单/向导。您应该根据请求的路径/请求参数/请求方法将方法映射到请求。因此,您可以根据需要定义视图列表并根据某些必需的步骤参数处理请求,而不是定义视图列表,也可以根据需要定义向导的步骤(命令对象也将更透明地处理)。以下是如何模拟经典的AWFC功能(这只是一个例子,你可以做更多的事情)。

A @Controller is a more flexible way to define a form / wizard. You are supposed to map methods to requests based on requested path / request parameters / request method. So instead of defining a list of views and processing the request based on some required "step" parameter, you can define the steps of your wizard as you wish (also the command object will be handled more transparently). Here's how you can get to emulate a classic AWFC functionality (this is only meant to be an example, there's a lot more you can do).

@Controller
@RequestMapping("/wizard.form")
@SessionAttributes("command")
public class WizardController {

    /**
     * The default handler (page=0)
     */
    @RequestMapping
    public String getInitialPage(final ModelMap modelMap) {
        // put your initial command
        modelMap.addAttribute("command", new YourCommandClass());
        // populate the model Map as needed
        return "initialView";
    }

    /**
     * First step handler (if you want to map each step individually to a method). You should probably either use this
     * approach or the one below (mapping all pages to the same method and getting the page number as parameter).
     */
    @RequestMapping(params = "_step=1")
    public String processFirstStep(final @ModelAttribute("command") YourCommandClass command,
                                   final Errors errors) {
        // do something with command, errors, request, response,
        // model map or whatever you include among the method
        // parameters. See the documentation for @RequestMapping
        // to get the full picture.
        return "firstStepView";
    }

    /**
     * Maybe you want to be provided with the _page parameter (in order to map the same method for all), as you have in
     * AbstractWizardFormController.
     */
    @RequestMapping(method = RequestMethod.POST)
    public String processPage(@RequestParam("_page") final int currentPage,
                              final @ModelAttribute("command") YourCommandClass command,
                              final HttpServletResponse response) {
        // do something based on page number
        return pageViews[currentPage];
    }

    /**
     * The successful finish step ('_finish' request param must be present)
     */
    @RequestMapping(params = "_finish")
    public String processFinish(final @ModelAttribute("command") YourCommandClass command,
                                final Errors errors,
                                final ModelMap modelMap,
                                final SessionStatus status) {
        // some stuff
        status.setComplete();
        return "successView";
    }

    @RequestMapping(params = "_cancel")
    public String processCancel(final HttpServletRequest request,
                                final HttpServletResponse response,
                                final SessionStatus status) {
        status.setComplete();
        return "canceledView";
    }

}

我试图改变方法签名这样你就可以了解我提到的灵活性。当然,还有更多内容:你可以在 @RequestMapping 中使用请求方法(GET或POST),你可以定义一个用<$ c注释的方法$ c> @InitBinder 等。

I tried to vary the method signatures so that you can get an idea about the flexibility I mentioned. Of course, there's a lot more to it: you can make use of request method (GET or POST) in the @RequestMapping, you can define a method annotated with @InitBinder, etc.

编辑:我有一个未映射的方法,我修复了(由方式,您需要确保没有模糊映射 - 可以映射到多个方法的请求 - 或未映射的请求 - 无法映射到任何方法的请求)。还要看一下@SessionAttributes,@ SessionStatus和@ModelAttribute,它们也是完全模拟经典AWFC行为所必需的(我已编辑代码以使其清晰)。

I had an unmapped method which I fixed (by the way, you need to make sure you don't have ambiguous mappings -- requests that could be mapped to more than one method -- or unmapped requests -- requests that cannot be mapped to any method). Also have a look at @SessionAttributes, @SessionStatus and @ModelAttribute, which are also needed for fully simulating the behaviour of the classic AWFC (I edited the code already to make this clear).

这篇关于AbstractWizardFormController使用Annotated @Controllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆