Spring MVC PropertiesMethodNameResolverの例
PropertiesMethodNameResolver、define the mapping between the URL and method name explicitlyへの柔軟なMultiActionControllerメソッド名リゾルバー。 次の例を参照してください。
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");
}
}
2. プロパティMethodNameResolver
PropertiesMethodNameResolverを使用すると、対応するメソッド名に任意のURL名を簡単にマップできます。
add update delete list add
これで、URLは次のパターンでメソッド名にマップされます。
-
/customer/a.htm –> add() method
-
/customer/b.htm –> update() method
-
/customer/c.htm –> delete() method
-
/customer/d.htm –> list() method
-
/customer/whatever.htm –> add() method
Note
デフォルトでは、MultiActionControllerはInternalPathMethodNameResolverを使用して、URLを対応するメソッド名にマップします。