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は次のパターンでメソッド名にマップされます。
-
CustomerController –>/customer/ *
-
/customer/add.htm –> add()
-
/customer/delete.htm –> delete()
-
/customer/update.htm –> update()
-
/customer/list.htm –> list()
3. InternalPathMethodNameResolver
InternalPathMethodNameResolverは、URLをメソッド名にマップするためのデフォルトのMultiActionController実装です。 ただし、メソッド名にプレフィックスまたはサフィックスを追加することは引き続き許可されています。
これで、URLは次のパターンでメソッド名にマップされます。
-
CustomerController –>/customer/ *
-
/customer/add.htm –> testaddCustomer()
-
/customer/delete.htm –> testdeleteCustomer()
-
/customer/update.htm –> testupdateCustomer()
-
/customer/list.htm –> testlistCustomer()
注
注釈を使用すると、MultiActionControllerの構成がより簡単になります。詳細については、このMultiActionController annotation exampleにアクセスしてください。
ソースコードをダウンロード
ダウンロード–SpringMVC-MultiActionController-Example.zip(7KB)