Пример Spring MVC PropertiesMethodNameResolver

Spring MVC PropertiesMethodNameResolver пример

PropertiesMethodNameResolver, гибкий преобразователь имен методовMultiActionController, вdefine the mapping between the URL and method name explicitly. Смотрите следующий пример:

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. PropertiesMethodNameResolver

С помощьюPropertiesMethodNameResolver вы можете легко сопоставить любое имя URL-адреса с именем метода:



 

 
   
    
      
    
       add
       update
       delete
       list
       add
    
       
     
    
  

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

  1. /customer/a.htm –> add() method

  2. /customer/b.htm –> update() method

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

  4. /customer/d.htm –> list() method

  5. /customer/whatever.htm –> add() method

Note
По умолчаниюMultiActionController используетсяInternalPathMethodNameResolver для сопоставления URL-адреса с соответствующим именем метода.

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