2011-01-07 10 views
26

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

春の私の限られた知識を考慮するのに役立つ例やポインタです。

答えて

61

@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)を利用することができ、あなたはEDIT

@InitBinderアノテーションを付けるメソッドを定義することができますこれは私が修正したものです(ところで、あいまいなマッピングがないことを確認する必要があります。複数のメソッドにマッピングできるリクエスト、つまりマッピングされていないリクエスト)。どのメソッドにもマッピングできないリクエストです。 @SessionAttributes、@SessionStatus、@ModelAttributeもご覧ください。これは、古典的なAWFCの動作を完全にシミュレートするためにも必要です(私はすでにこのコードを編集しています)。

+5

+1これは、Springのドキュメンテーション/サンプルアプリケーションが提供するものよりも確かに良い説明です。多くの人がこれを参考にしています。 – prasopes

+0

本当に助かりました。 – false9striker

+1

これは便利ですが、 'pageViews'メソッドの説明はありません。 – djangofan

関連する問題