Spring MVC ParameterMethodNameResolver example

ParameterMethodNameResolver, a MultiActionController method name resolver to map URL to method name via request parameter name, and the parameter name is customizable through the “paramName” property. See following example :

1. MultiActionController

A MultiActionController example.

package com.mkyong.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

With ParameterMethodNameResolver configured, and define the parameter name thought the “paramName” property:

<beans ...>

 <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

  <bean class="com.mkyong.common.controller.CustomerController">
     <property name="methodNameResolver">
    <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
       <property name="paramName" value="action"/>
    </bean>
     </property>
   </bean>

</beans>

Now, the URL will map to the method name via the “action” request parameter name :

    /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
By default, MultiActionController is used the InternalPathMethodNameResolver to map URL to the corresponds method name.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值