zoukankan      html  css  js  c++  java
  • RequestMapping注解

    1、简介

    @RequestMapping注解是springmvc中常用的注解,它是用来处理请求映射地址的注解,可以用于类或方法上,用于类上时,表示该类中的方法后以该地址为父路径。

    2、属性

    • value:指定请求的地址,地址可以分为三类

    1、指定具体的路径

        @RequestMapping(value= {"/index"})
        public String index() {
    	    return "index";
        }
    

    2、指定为含有某变量的一类值(URI模板)当控制器处理请求时,URI模板中的变量会被对应部分的值所填充,具体请看示例

        //ownerId即为变量,若请求地址为“/index/admin”,即变量id值为admin
        @RequestMapping(value="/index/{id}", method=RequestMethod.GET)
        public String findOwner(@PathVariable String id) {
            return "index";
        }
    

    3、指定为含正则表达式的一类值(这个实际开发中应该很少用)

        @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}{extension:\.[a-z]+}")
            public void handle(@PathVariable String version, @PathVariable String extension) {
                // 代码部分省略...
            }
        }    
    
    • method:指定提交请求的方式,有GET,POST等
      示例

        @RequestMapping(value= {"/index"},method=RequestMethod.POST)
        public String index() {
            return "index";
        }  
      
    • consmues:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html,请求类型与指定类型不同时,将无法请求
      示例

        @RequestMapping(value= {"/index"},method=RequestMethod.POST,consumes="application/json")
        	public String index() {
        		return "index";
        	}
      
    • produces:指定返回的内容类型,请求头的Accept中要包含该类型,才可以返回
      示例

        @RequestMapping(value= {"/index"},method=RequestMethod.POST,produces="application/json")
        	public String index() {
        		return "index";
        	}
      
    • params:表示request请求中必须包含某些参数,方法才会进行请求处理
      示例
      请求中必须包含username这个参数,方法才会对请求进行处理

        @RequestMapping(value= {"/index"},method=RequestMethod.GET,params="username")
        public String index() {
        		return "index";
        }
      
    • headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
      示例
      请求头中的 referer需与 “http://www.baidu.com/”一样

        @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.baidu.com/")
          public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
            // implementation omitted
          }
        }
  • 相关阅读:
    rails best particseceeeeeeeeeeee
    clear out all firefox plugin
    named scope on rail3
    javascript保留两位小数
    rails
    CVSNT权限配置
    rails session使用好文章
    rails session security
    javascript断点调试方法
    rails3发邮件
  • 原文地址:https://www.cnblogs.com/hamawep789/p/10849818.html
Copyright © 2011-2022 走看看