Пример Spring MVC MultiActionController

Пример 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 будет сопоставлен с именем метода в следующих шаблонах:

  1. CustomerКонтроллер ->/customer/ *

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

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

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

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

3. InternalPathMethodNameResolver

InternalPathMethodNameResolver - это реализацияMultiActionController по умолчанию для сопоставления URL-адреса с именем метода. Но вы все еще можете добавить префикс или суффикс к имени метода:


 

  
     
    
       
       
    
     
   

Теперь URL-адрес будет сопоставлен с именем метода в следующем шаблоне:

  1. CustomerКонтроллер ->/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.

Скачать исходный код