gpt4 book ai didi

java - AbstractWizardFormController 使用带注释的@Controllers

转载 作者:IT老高 更新时间:2023-10-28 13:45:27 25 4
gpt4 key购买 nike

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

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

最佳答案

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

@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";
}

}

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

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

关于java - AbstractWizardFormController 使用带注释的@Controllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4623667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com