获取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