码迷,mamicode.com
首页 > 移动开发 > 详细

springMVC的@RequestMapping的参数和用法

时间:2016-06-17 00:44:56      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

 

RequestMapping注解里面包含的参数如图

 

技术分享

value: 控制器名字也是url的映射

params:url中的参数,可以是多个

method: http请求的动作. get  post  put delete...

headers : 请求头参数

@PathVariable :映射 URL 绑定占位  ( 映射URL路径里面的变量)

 

@Controller
//映射 控制器类
@RequestMapping("/hellotest")
public class Hello2 {
    public final static String DISPATCHER  ="show";
    
    @RequestMapping("/test")
    public String testUrl(){
        
        System.out.println("testUrl=http://localhost:8080/springMVC/hellotest/test");
        return DISPATCHER;
    }
     /**
      * get动作请求的测试
      * @return
      */
    @RequestMapping(method=RequestMethod.GET,value="/testMethodGet")
    public String testMethodGet(){
        System.out.println("get..............");
        return DISPATCHER;
    }
      /**
     * post动作请求的测试
     * @return
     */
    @RequestMapping(method=RequestMethod.POST,value="/testMethodPost")
    public String testMethodPost(){
        System.out.println("post..............");
        return DISPATCHER;
    }
    /**
     * 测试url中带参数(未测试请求头信息)
     * @return
     */
    @RequestMapping(value="/testHeadersAndParams",
            params={"username","sex!=1","password=123"},
            method=RequestMethod.GET)
    public String testHeadersAndParams(){
        //url http://localhost:8080/springMVC/hellotest/testHeadersAndParams?username=10&password=123&sex=2
        System.out.println("testHeadersAndParams");
        return DISPATCHER;
    }
    //PathVariable 映射 URL 绑定占位  ( 映射URL路径里面的变量)
    @RequestMapping(value="/testPathVariable/{id}",method=RequestMethod.GET)
    public String testPathVariable(@PathVariable("id") Integer id){
        System.out.println("testPathVariable  id:"+id);
        //http://localhost:8080/springMVC/hellotest/testPathVariable/5
        return DISPATCHER;
    }

    @RequestMapping(value="/testPathVariable2/{name}",method=RequestMethod.GET)
    public String testPathVariable2(@PathVariable("name") String name){
        System.out.println("testPathVariable  name :"+name);
        // http://localhost:8080/springMVC/hellotest/testPathVariable2/str
      return DISPATCHER;
    }
    @RequestMapping()
    public String test(){
         return DISPATCHER;
    }
}

 

springMVC的@RequestMapping的参数和用法

标签:

原文地址:http://www.cnblogs.com/sunjiqiang/p/5592549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!