SpringMVC是如何查找方法的参数名的?
2014-08-21 16:18
92 查看
项目中用到SpringMVC,常见的用法像这样
当GET http://localhost:8080/test/hello?name=aducode请求到服务端时, SpringMVC的 DispatcherServlet处理请求,并根据URL找到@RequestMapping对应的方法,然后根据
@RequestParameter("name") 将url中的name值传递给方法调用
但是Spring还支持默认参数名
@Controller @RequestMapping("test") class Test{ @RequestMapping("/hello") @ResponseBody public Object test(@RequestParameter("name") String name){ return "hello! "+name; } }
当GET http://localhost:8080/test/hello?name=aducode请求到服务端时, SpringMVC的 DispatcherServlet处理请求,并根据URL找到@RequestMapping对应的方法,然后根据
@RequestParameter("name") 将url中的name值传递给方法调用
但是Spring还支持默认参数名
@Controller @RequestMapping("test") class Test{ @RequestMapping("/hello") @ResponseBody public Object test(String name){ return "hello! "+name; } }即不适用@RequestParameter注解,昨天有人问我这种情况是如何找到url中对应参数的。考虑了一下,使用java的反射机制是不能获取参数名这样的信息的,于是大概查看了一下spring的源码,发现是使用了 org.springframework.core.LocalVariableTableParameterNameDiscoverer 这个类,原理就是读取class字节码,解析其中中的LocalVariableTable,得到方法的参数名称。这样的前提是java编译成class时,必须开启debug,如果关闭debug,就会失效。测试结果如下:
相关文章推荐
- springMVC正确使用GET POST PUT和DELETE方法,如何传递参数
- 21. SpringMVC_如何确定目标方法POJO类型参数(无写笔记)
- 8.SpringMVC_如何确定目标方法POJO类型参数/视图解析流程分析/
- 如何查找系统屏幕参数
- 如何使用web_reg_save_param方法保存的多个参数?
- 如何在类图中显示带参数的方法
- 如何用Class_forName方法动态加载构造方法带参数的类
- Jsp中通过get方法传递参数为中文时出现乱码如何解决?
- 数组如何作为方法的参数
- 如何在MapX下读取属性值、自定义范围专题图、在mapx中查找对象的方法
- 如何使用web_reg_save_param方法保存的多个参数?
- ExtJs 中使用 TaskRunner 和 DelayedTask 如何传参数到 run() 方法
- Server.Transfer 方法如何传递复杂的参数
- 如何使用web_reg_save_param方法保存的多个参数?
- 如何使用web_reg_save_param方法保存的多个参数?
- 如何C#中实现在TreeView查找某一节点(两种方法)
- 如何使用web_reg_save_param方法保存的多个参数?
- 如何使用web_reg_save_param方法保存的多个参数?
- 如何使用web_reg_save_param方法保存的多个参数?
- 如何把方法(函数)当参数传递