Spring MVC ParameterMethodNameResolver пример
ParameterMethodNameResolver, преобразователь имени метода MultiActionController вmap URL to method name via request parameter name, а имя параметра можно настроить с помощью свойства «paramName». Смотрите следующий пример:
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. ParameterMethodNameResolver
С настроеннымParameterMethodNameResolver и определите имя параметра с помощью свойства «paramName»:
Теперь URL будет отображаться на имя метода через имя параметра запроса «action»:
-
/customer/*.htm?action=add –> add() method
-
/customer/whatever.htm?action=add –> add() method
-
/customer/*.htm?action=update –> update() method
-
/customer/*.htm?action=delete –> delete() method
-
/customer/*.htm?action=list –> list() method
P.S the “*” means any text.
Note
По умолчаниюMultiActionController используетсяInternalPathMethodNameResolver для сопоставления URL-адреса с соответствующим именем метода.
Скачать исходный код
Скачать -SpringMVC-ParameterMethodNameResolver-Example.zip (7KB)