Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AbstractWizardFormController使用带注释的@Controller_Java_Spring_Spring Mvc_Spring Annotations - Fatal编程技术网

Java AbstractWizardFormController使用带注释的@Controller

Java AbstractWizardFormController使用带注释的@Controller,java,spring,spring-mvc,spring-annotations,Java,Spring,Spring Mvc,Spring Annotations,在Spring框架中,AbstractWizardFormController似乎已被弃用。如何在SpringMVC框架中实现多页表单。(我没有使用webflow) 考虑到我在Spring的有限知识,任何示例或指针都会有所帮助。A@Controller是定义表单/向导的更灵活的方法。您应该根据请求的路径/请求参数/请求方法将方法映射到请求。因此,您可以根据需要定义向导的步骤,而不是定义视图列表并根据某些必需的“步骤”参数处理请求(命令对象的处理也将更加透明)。以下是如何模拟经典AWFC功能(这只

在Spring框架中,
AbstractWizardFormController
似乎已被弃用。如何在SpringMVC框架中实现多页表单。(我没有使用webflow)


考虑到我在Spring的有限知识,任何示例或指针都会有所帮助。

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

我尝试改变方法签名,以便您了解我提到的灵活性。当然,还有很多:您可以在
@RequestMapping
中使用request方法(GET或POST),您可以定义一个用
@InitBinder
注释的方法,等等


编辑:我有一个未映射的方法,我修复了它(顺便说一句,您需要确保没有不明确的映射—可以映射到多个方法的请求—或者未映射的请求—无法映射到任何方法的请求)。还可以看看@SessionAttributes、@SessionStatus和@ModelAttribute,它们也是完全模拟经典AWFC行为所必需的(我已经编辑了代码以澄清这一点)。

+1这肯定比Spring文档/示例应用程序提供的解释更好。很多人会觉得这很有帮助。这很有帮助,但是没有解释
pageViews
方法。
@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";
    }

}