Spring MVC MultiActionControllerの例

Spring MVC MultiActionControllerの例

Spring MVCアプリケーションでは、MultiActionControllerを使用して関連するアクションを単一のコントローラーにグループ化します。メソッドハンドラーは以下のシグネチャに従う必要があります。

    public (ModelAndView | Map | String | void) actionName(
        HttpServletRequest, HttpServletResponse [,HttpSession] [,CommandObject]);

1. MultiActionController

MultiActionControllerの例を参照してください。

package com.example.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class CustomerController extends MultiActionController{

    public ModelAndView add(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","add() method");

    }

    public ModelAndView delete(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","delete() method");

    }

    public ModelAndView update(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","update() method");

    }

    public ModelAndView list(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerPage", "msg","list() method");

    }

}

ControllerClassNameHandlerMappingが構成されています。



 

  

2. マッピングの例

これで、要求されたURLは次のパターンでメソッド名にマップされます。

  1. CustomerController –>/customer/ *

  2. /customer/add.htm –> add()

  3. /customer/delete.htm –> delete()

  4. /customer/update.htm –> update()

  5. /customer/list.htm –> list()

3. InternalPathMethodNameResolver

InternalPathMethodNameResolverは、URLをメソッド名にマップするためのデフォルトのMultiActionController実装です。 ただし、メソッド名にプレフィックスまたはサフィックスを追加することは引き続き許可されています。


 

  
     
    
       
       
    
     
   

これで、URLは次のパターンでメソッド名にマップされます。

  1. CustomerController –>/customer/ *

  2. /customer/add.htm –> testaddCustomer()

  3. /customer/delete.htm –> testdeleteCustomer()

  4. /customer/update.htm –> testupdateCustomer()

  5. /customer/list.htm –> testlistCustomer()


注釈を使用すると、MultiActionControllerの構成がより簡単になります。詳細については、このMultiActionController annotation exampleにアクセスしてください。

ソースコードをダウンロード

ダウンロード–SpringMVC-MultiActionController-Example.zip(7KB)