Spring MVC PropertiesMethodNameResolver example
2015-09-13 15:07
423 查看
PropertiesMethodNameResolver, a flexible
MultiActionControllermethod name resolver, to define the mapping between the URL and method name explicitly. See following example :
1. MultiActionController
A MultiActionControllerexample.
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. PropertiesMethodNameResolver
With PropertiesMethodNameResolver, you can map whatever URL name to corresponds method name easily :
<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.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/customer/a.htm">add</prop> <prop key="/customer/b.htm">update</prop> <prop key="/customer/c.htm">delete</prop> <prop key="/customer/d.htm">list</prop> <prop key="/customer/whatever.htm">add</prop> </props> </property> </bean> </property> </bean> </beans>
Now, the URL will map to the method name in the following pattern :
/customer/a.htm –> add() method /customer/b.htm –> update() method /customer/c.htm –> delete() method /customer/d.htm –> list() method /customer/whatever.htm –> add() method
Note
By default,
MultiActionControlleris used the
InternalPathMethodNameResolverto map URL to the corresponds method name.
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- 分享微信开发Html5轻游戏中的几个坑
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- VBScript 剪贴板抓取URL并在浏览器中打开
- 用vbs 实现从剪贴板中抓取一个 URL 然后在浏览器中打开该 Web 站点
- asp获取URL参数的几种方法分析总结[原创]_应用技巧_脚本之家
- zend framework框架中url大小写问题解决方法
- Zend的MVC机制使用分析(二)
- url decode problem 解决方法
- php url地址栏传中文乱码解决方法集合
- 探讨:parse url解析URL,返回其组成部分
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.NET MVC中将控制器分离到类库的实现
- C#实现自动识别URL网址的方法
- C#自定义针对URL地址的处理类实例
- asp.net实现在非MVC中使用Razor模板引擎的方法
- ASP.NET MVC中的AJAX应用
- ASP让url的中文显示为编码