在Spring Framework中,AbstractWizardFormController
似乎不推荐使用。如何在Spring MVC框架中实现多个页面。 (我没有使用webflow)使用带批注的@Controllers的AbstractWizardFormController
任何示例或指针都有助于考虑我在Spring中的有限知识。
在Spring Framework中,AbstractWizardFormController
似乎不推荐使用。如何在Spring MVC框架中实现多个页面。 (我没有使用webflow)使用带批注的@Controllers的AbstractWizardFormController
任何示例或指针都有助于考虑我在Spring中的有限知识。
@Controller是一种更灵活的方式来定义窗体/向导。您应该根据请求的路径/请求参数/请求方法将方法映射到请求。因此,您可以根据需要定义您的向导步骤(也可以更透明地处理命令对象),而不是定义视图列表并处理基于某些必需的“步骤”参数的请求。以下是您如何模拟经典的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";
}
}
我试着改变方法签名,以便您可以了解我提到的灵活性。当然,还有很多更给它:你可以在@RequestMapping
使用请求方法(GET或POST),你可以定义@InitBinder
注释的方法等
编辑:我有一个映射的方法我解决了这个问题(顺便说一下,您需要确保您没有模糊的映射 - 可以映射到多个方法的请求 - 或未映射的请求 - 不能映射到任何方法的请求)。还可以看看@SessionAttributes,@SessionStatus和@ModelAttribute,它们也是完全模拟经典AWFC行为所需要的(我已经编辑了代码,以使其清晰)。
+1这肯定比Spring文档/示例应用程序提供的更好的解释。很多人都觉得这很有帮助。 – prasopes 2011-01-07 11:08:55