springMVC-@RequestMapping注解

来源:互联网 发布:centos安装nginx 编辑:程序博客网 时间:2024/05/22 16:38

@RequestMapping注解用来匹配访问路径与处理请求的逻辑方法,不仅可以修饰方法也可以修饰Controller类。

@RequestMapping("/springmvc")@Controllerpublic class SpringMVCTest {    private static final String SUCCESS = "success";    @RequestMapping("/testRequestMapping")    public String testRequestMapping(){System.out.println("testRequestMapping");return SUCCESS;<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span>}</span>
    @RequestMapping(value="testMethod",method=RequestMethod.POST)    public String testMethod(){
<span style="white-space:pre"></span>System.out.println("testRequestMapping");<span style="white-space:pre"></span>return SUCCESS;    }}
①当类与方法同时被@RequestMapping修饰,以testRequestMapping方法为例,则访问路径为  ${content root}/springmvc/testRequestMapping,

 修饰类的@RequestMapping中的路径作为工程访问的根路径,如果只修饰方法,则修饰方法的路径为工程访问的根路径则访问路径为  ${content root}/testRequestMapping。

②@RequestMapping注解的 method 属性可以限制请求的方法,当method=“POST” 时,只能处理post请求而不能处理get请求。


0 0
原创粉丝点击