abstractcontroller java中的作用_java – AbstractWizardFormController使用注释@Controllers

@Controller是一种更灵活的方式来定义表单/向导。你应该根据请求的路径/请求参数/请求方法映射方法到请求。因此,您可以根据需要定义向导的步骤(也可以更透明地处理命令对象),而不是定义视图列表并根据一些必需的“step”参数处理请求。这里是如何可以模拟经典的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的行为(我编辑的代码已经使这一点清楚)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值