• SpringMVC-@RequestMapping的参数和用法


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

    这里写图片描述

    RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

    @RequestMapping 除了修饰方法, 还可来修饰类 :

    类定义处: 提供初步的请求映射信息。相对于 WEB 应用的根目录;

    方法处: 提供进一步的细分映射信息。 相对于类定义处的 URL。

    若类定义处未标注 @RequestMapping,则方法处标记的 URL相对于 WEB 应用的根目录

    返回ModelAndView时的url会根据你的 @RequestMapping实际情况组成。 
    如果类上没有映射,那么url直接就是方法的映射;否则url为类上+方法上映射路径组合。

    对应项目jsp位置则是一级路径对应一级文件目录。

    如url为/default/index对应项目中webapp/default/index.jsp


    RequestMapping注解有六个属性,下面我们把她分成三类进行说明。

    【1、 value, method;】

    value:指定请求的实际地址,指定的地址可以是URI Template 模式;

    method: 指定请求的method类型, GET、POST、PUT、DELETE等;


    【2、consumes,produces;】

    consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

    produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;


    【3、 params,headers;】

    params: 指定request中必须包含某些参数值时,才让该方法处理。

    headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。


    测试示例如下:

    【1】value

    • JSP 页面
    <a href="springmvc/testRequestMapping">Test RequestMapping</a>
    • 1
    • controller
        @RequestMapping(value="/testRequestMapping" )
        public String testRequestMapping() {
            System.out.println("testRequestMapping");
            return SUCCESS;
        }
    • 1
    • 2
    • 3
    • 4
    • 5

    成功返回success.jsp 。

    Tips :若 href 属性值,不等于value值,则将提示404错误。


    value的uri值为以下三类:

    A) 可以指定为普通的具体值;

    如下:

    @RequestMapping("/testRequestMapping")
    • 1

    B) 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables)–restful风格;

        @RequestMapping("/testPathVariable/{id}")
        public String testPathVariable(@PathVariable Integer id2) {
            System.out.println("testPathVariable: " + id2);
            return SUCCESS;
        }
    • 1
    • 2
    • 3
    • 4
    • 5

    其中关于@PathVariable 有如下说明:

    ① 如果路径中的变量与方法中的变量名一致,可直接使用@PathVariable;

    ② 如果二者不一致,则使用@PathVariable(Variable)显示指定要绑定的路径中的变量 。

    @PathVariable只能绑定路径中的占位符参数,且路径中必须有参数。

    @PathVariable用法参考路径参数绑定参考

        @RequestMapping("/testPathVariable/{id}")
        public String testPathVariable(@PathVariable("id") Integer id2) {
            System.out.println("testPathVariable: " + id2);
            return SUCCESS;
        }
        //路径中的 id 与 方法中的 id2 绑定
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);

    @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:d.d.d}.{extension:.[a-z]}")  
      public void handle(@PathVariable String version, @PathVariable String extension) {      
        // ...  
      }  
    }  
    • 1
    • 2
    • 3
    • 4
    • 5

    【2】method

    • JSP 页面
    <a href="springmvc/testMethod">Test Method</a>
    //href 默认为get 请求。
    • 1
    • 2
    • controller–限制接收post 请求。
        @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
        public String testMethod() {
            System.out.println("testMethod");
            return SUCCESS;
        }
    • 1
    • 2
    • 3
    • 4
    • 5
      • result as follows :

    HTTP Status 405 - Request method ‘GET’ not supported 。

    这里写图片描述

    【状态码405表示:请求中指定的方法不被允许。】

    将method 改为method = RequestMethod.GET正常跳转页面。


    【3】consumes

    • JSP 页面

    仍以testMethod为例,提交表单。

    默认contentType为Content-Type:application/x-www-form-urlencoded。

        <form action="springmvc/testMethod" method="POST">
            <input type="text" name="username" value=""/>
            <input type="submit" value="submit"/>
        </form>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这里写图片描述


    • controller–限制接收post 请求以及consumes="application/json"
        @RequestMapping(value = "/testMethod", method = RequestMethod.POST,consumes="application/json")
        public String testMethod() {
            System.out.println("testMethod");
            return SUCCESS;
        }
    • 1
    • 2
    • 3
    • 4
    • 5
      • result as follows :

    这里写图片描述

    【状态码415表示:由于媒介类型不被支持,服务器不会接受请求。。】

    去掉 consumes属性,页面正常跳转 !


    【4】produces

    后台代码如下:

    @RequestMapping(value = "/testMethod", method = RequestMethod.POST,produces="application/json")
        public void testMethod2(HttpServletRequest request,HttpServletResponse response,Model model) 
        throws IOException {
            request.getHeader("Accept");
            System.out.println(request.getHeader("Accept"));
    //      response.setContentType("application/json");
            String username = request.getParameter("username");
            System.out.println("testMethod..."+username);
            model.addAttribute("user", username);
            Object jsonString =  "{'name': 'helloworlda'}";
            JSONObject jsonobj=JSONObject.fromObject(jsonString);
            PrintWriter out = response.getWriter();
            out.print(jsonobj);
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 浏览器请求头
    text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    • 1

    其中最后一项 : */*;q=0.8

    该项表明可以接收任何类型的,权重系数0.8表明如果前面几种类型不能正常接收。则使用该项进行自动分析。

    application/json 几种主流浏览器都可以自动解析。


    【5】params

    • JSP页面
        form action="springmvc/testParamsAndHeaders" method="POST">
            <input type="text" name="username" value=""/>
            <input type="text" name="age" value=""/>
            <input type="submit" value="submit"/>
        </form>
    • 1
    • 2
    • 3
    • 4
    • 5

    参数 username=tom;age = 10;

    • 后台代码:

    设定必须包含username 和age两个参数,且age参数不为10 (可以有多个参数)。

    @RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" })
        public String testParamsAndHeaders() {
            System.out.println("testParamsAndHeaders");
            return SUCCESS;
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • result as follows :

    这里写图片描述

    【状态码400表示:服务器未能理解请求。 】

    • 将age 改为其他值,正常跳转。

    这里写图片描述


    【6】headers

    • 浏览器请求头如下:

    这里写图片描述

    • 后台测试代码如下:
    @RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" }, headers = { "Accept-Language=US,zh;q=0.8" })
        public String testParamsAndHeaders() {
            System.out.println("testParamsAndHeaders");
            return SUCCESS;
        }
    • 1
    • 2
    • 3
    • 4
    • 5

    设定请求头中第一语言必须为US。

    • result as follows :

    这里写图片描述

    【状态码404表示:服务器无法找到被请求的页面。】

    将后台代码改为zh-CN。。。

    这里写图片描述

    页面正常跳转。

  • 相关阅读:
    首页调单个产品分类的推荐产品,最新产品和热卖商品
    ecshop模板<! TemplateBeginEditable name="左上角主区域" >用法
    复制DataTable数据到新DataTable
    定时任务时间与当前时间比较的方法
    批量删除文件夹下包含指定字段的文件
    SQL 字符串去除空格函数
    Javascript的IE和Firefox(火狐)兼容性的常用例子
    查询表某列的加权平均值
    Jquery实现页面定时跳转
    Date.parse Firefox返回Nan的解决办法
  • 原文地址:https://www.cnblogs.com/yachao1120/p/8884833.html
Copyright © 2020-2023  润新知