zoukankan      html  css  js  c++  java
  • springmvc接收参数

    springmvc 接收请求信息


    1.@RequestHeader
        @RequestHeader注解用于映射请求头数据到Controller方法的对应参数
        
        下面是一个请求头
     

        Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
        Accept-Encoding:gzip, deflate, br
        Accept-Language:zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
        Cache-Control:max-age=0
        Connection:keep-alive
        Host:localhost:8080
        Upgrade-Insecure-Requests:1
        User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36
    
        @GetMapping("/getRequestHeader")
        public Result getRequestHeader(@RequestHeader("Accept-Encoding") String encoding) {
            return ResultUtil.successResult(encoding);
        }


    2.@CookieValue  
        可以把Request header中关于cookie的值绑定到方法的参数上

      

     @RequestMapping("/getCookie")
        public Result getCookie(@CookieValue("JSESSIONID") String co) {
            return ResultUtil.successResult(co);
        }

        即把JSESSIONID的值绑定到参数co上

    3.URL模板变量 
      

     @RequestMapping("show5/{id}/{name}")
        public ModelAndView test5(@PathVariable("id") Long ids ,@PathVariable("name") String names){
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg","占位符映射:id:"+ids+";name:"+names);
            mv.setViewName("hello2");
            return mv;
        }


    4.@RequestParam
        @RequestParam可以接受简单类型的属性,也可以接受对象类型。@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。@RequestParam也可用于其它类型的请求,例如:POST、DELETE等请求。

    @RequestParam(value = "loginFlag", required = true,defaultValue = "aa") String loginFlag)

        (1)value:请求参数名
        
        (2)required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)
        
        (3)defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false(可选配置)


    5.@RequestBody 接收
        @RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/json、application/xml等类型的数据。
       

    public Map<String, Map<String, Object>> saveParamaters(@RequestBody Map<String, Object> map)


    6.对象接收(默认为接收application/x-www-form-urlencoded,加上@RequestBody接收application/json、application/xml等)
        

    @GetMapping(path = "/get2")
        public void get2(UserVo vo) {
            log.info("name:{},age:{}", vo.getName(), vo.getAge());
        }



    7.使用HttpServletRequest的方法 不推荐
      

     @GetMapping(path = "/get3")
        public void get3(HttpServletRequest request) {
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            log.info("name:{},age:{}", name, age);
        }
  • 相关阅读:
    如何只通过Sandboxed Solution启动一个定时执行的操作
    创建与SharePoint 2010风格一致的下拉菜单 (续) 整合Feature Custom Action框架
    创建与SharePoint 2010风格一致的下拉菜单
    《SharePoint 2010 应用程序开发指南》第二章预览
    SharePoint 2013 App 开发 (1) 什么是SharePoint App?
    使用Jscex增强SharePoint 2010 JavaScript Client Object Model (JSOM)
    搜索范围的管理
    SharePoint 2010 服务应用程序(Service Application)架构(1)
    SharePoint 2010 服务应用程序(Service Application)架构(2)
    SharePoint 2013 App 开发 (2) 建立开发环境
  • 原文地址:https://www.cnblogs.com/jthr/p/13476648.html
Copyright © 2011-2022 走看看