Пример Spring MVC MultiActionController
В приложении Spring MVCMultiActionController используется для группировки связанных действий в один контроллер, обработчик метода должен следовать подписи ниже:
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 будет сопоставлен с именем метода в следующих шаблонах:
-
CustomerКонтроллер ->/customer/ *
-
/customer/add.htm –> add()
-
/customer/delete.htm –> delete()
-
/customer/update.htm –> update()
-
/customer/list.htm –> list()
3. InternalPathMethodNameResolver
InternalPathMethodNameResolver - это реализацияMultiActionController по умолчанию для сопоставления URL-адреса с именем метода. Но вы все еще можете добавить префикс или суффикс к имени метода:
Теперь URL-адрес будет сопоставлен с именем метода в следующем шаблоне:
-
CustomerКонтроллер ->/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)