zoukankan      html  css  js  c++  java
  • 0057 Spring MVC如何获取HTTP请求头信息--URL中的动态参数--@RequestHeader--@CookieValue--@PathVariable

    获取HTTP请求头信息

    一个HTTP请求除了有参数和实体内容外还有HTTP请求头信息,Spring MVC也可以获取这部分信息
    @RequestHeader解可以将请求头信息映射到处理方法的形参上
    @CookieValue注解可以将Cookie映射到形参上

    写个controller

    package net.sonng.mvcdemo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.CookieValue;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class HeaderController {
        @RequestMapping(value="/getHeader",method=RequestMethod.GET)
        public ModelAndView getHeader(
                @RequestHeader("Host") String host,
                @RequestHeader("User-Agent") String userAgent,
                @RequestHeader("Accept") String accept,
                @RequestHeader("Accept-Language") String acceptLanguage,
                @RequestHeader("Accept-Encoding") String acceptEncoding,
                @RequestHeader("Cookie") String cookie,
                @RequestHeader("Connection") String conn,
                @CookieValue("JSESSIONID") String cookie2){
            //@RequestHeader将http请求头信息赋值给形参
            //@CookieValue直接将请求头中的cookie的值赋值给形参
            ModelAndView mav=new ModelAndView();
            mav.addObject("host", host);
            mav.addObject("userAgent", userAgent);
            mav.addObject("accept", accept);
            mav.addObject("acceptLanguage", acceptLanguage);
            mav.addObject("acceptEncoding", acceptEncoding);
            mav.addObject("cookie", cookie);
            mav.addObject("conn", conn);
            mav.addObject("cookie2", cookie2);
            mav.setViewName("result");  //返回值是个字符串,就是视图名
            return mav;
        }
    }
    
    

    写个result.jsp作视图

    <%@page pageEncoding="utf-8" 
            contentType="text/html;charset=utf-8" %>
    <html>
        <head>
            <title>返回结果</title>
        </head>
        <body style="color:blue;font-size:18px">
            您的请求头信息为:<br>
            Host: ${host }<br><br><br>
            User-Agent: ${userAgent }<br><br><br>
            Accept: ${accept }<br><br><br>
            Accept-Language: ${acceptLanguage }<br><br><br>
            Accept-Encoding: ${acceptEncoding }<br><br><br>
            Cookie: ${cookie }<br><br><br>
            Connection: ${conn }<br><br><br>
            cookie2:${cookie2 }<br><br><br>
        </body>
    </html> 
    

    直接访问,可以看到jsp页面列出了这个http请求的头信息和cookie

    您的请求头信息为:
    Host: localhost:8080
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
    Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    Cookie: {JSESSIONID=javax.servlet.http.Cookie@3c915cc} //奇怪的是,输出的是个Cookie对象
    Connection: keep-alive
    cookie2:304FDCF278E823F2F2B8850BD7B8B8DC

    获取请求URL中的动态参数

    @PathVariable注解可以获取请求URL中的动态参数,这样就把url中“?”后面或者http实体中的部分参数,转移到了一个url中,多个url就映射到了一个方法上

    写个Controller

    package net.sonng.mvcdemo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class PathVariableTestController {
        @RequestMapping("/book/{isbn}") //@PathVariable把url中的动态参数传递给了形参,这里的{isbn}貌似还支持正则
        public String path(@PathVariable("isbn") String bookid,Model model){
             model.addAttribute("isbn", bookid);
             return "result";
        }
    }
    

    视图result.jsp

    <%@page pageEncoding="utf-8" 
            contentType="text/html;charset=utf-8" %>
    <html>
        <head>
            <title>返回结果</title>
        </head>
        <body style="color:blue;font-size:18px">
            您请求的书籍的ISBN为:${isbn }
        </body>
    </html> 
    

    直接请求:http://localhost:8080/mvcdemo/book/9787111336242 ,输出如下:

    您请求的书籍的ISBN为:9787111336242

  • 相关阅读:
    寒武纪芯片——有自己的SDK,支持tf、caffe、MXNet
    专车降价滴滴快车使命终结?
    滴滴代驾骑虎难下 司机1人23部手机刷单
    柳青:再见!滴滴打车 其实这里面都是故事
    “专车”监管意见最快本月公布
    不少专车司机考虑退出
    专车新规或下周发布,估计有大量司机流失
    英媒:滴滴和优步每年烧钱64亿
    约租车管理办法近期公布 数量或受地方政府管控
    Uber上海公司被司机打上门
  • 原文地址:https://www.cnblogs.com/sonng/p/6645834.html
Copyright © 2011-2022 走看看