Spring MVC ParameterMethodNameResolverの例

Spring MVC ParameterMethodNameResolverの例

ParameterMethodNameResolvermap URL to method name via request parameter nameへのMultiActionControllerメソッド名リゾルバー。パラメーター名は「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」リクエストパラメータ名を介してメソッド名にマッピングされます。

  1. /customer/*.htm?action=add –> add() method

  2. /customer/whatever.htm?action=add –> add() method

  3. /customer/*.htm?action=update –> update() method

  4. /customer/*.htm?action=delete –> delete() method

  5. /customer/*.htm?action=list –> list() method

P.S the “*” means any text.

Note
デフォルトでは、MultiActionControllerInternalPathMethodNameResolverを使用して、URLを対応するメソッド名にマップします。

ソースコードをダウンロード