31

Spring Framework では、AbstractWizardFormController非推奨のようです。Spring MVC フレームワークで複数ページ フォームを実装する方法。(私はウェブフローを使用していません)

Spring に関する私の知識が限られていることを考えると、どのような例や指針も役立つでしょう。

4

1 に答える 1

68

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

}

私が言及した柔軟性についてのアイデアを得ることができるように、メソッド シグネチャを変更しようとしました。もちろん、それだけではありません。 でリクエスト メソッド (GET または POST) を使用したり、@RequestMappingで注釈を付けたメソッドを定義したりできます@InitBinder

編集:私は修正したマッピングされていないメソッドを持っていました(ちなみに、あいまいなマッピングがないことを確認する必要があります-複数のメソッドにマッピングできるリクエスト-またはマッピングされていないリクエスト-できないリクエスト任意のメソッドにマップされます)。また、@SessionAttributes、@SessionStatus、および @ModelAttribute も見てください。これらは、従来の AWFC の動作を完全にシミュレートするためにも必要です (これを明確にするために、既にコードを編集しました)。

于 2011-01-07T10:14:19.403 に答える