spring mvc controller中获取request head内容:
@RequestMapping("/{mlid}/{ptn}/{name}") public String print(@PathVariable Integer mlid, @PathVariable String ptn, @PathVariable String name, HttpSession session, Model model, @RequestHeader String referer, @RequestHeader ("Referer") String referer2, @RequestHeader ("host") String hostName, @RequestHeader ("Accept") String acceptType, @RequestHeader ("Accept-Language") String acceptLang, @RequestHeader ("Accept-Encoding") String acceptEnc, @RequestHeader ("Cookie") String cookie, @RequestHeader ("User-Agent") String userAgent, HttpServletRequest request) { System.out.println(hostName); // if (referer == null) { // return "common/blank"; // } String header = request.getHeader("Referer"); String header2 = request.getHeader("referer"); System.out.println(header); System.out.println(header2);
@RequestHeader String referer,就是@RequestHeader ("Referer") String referer2, 变量referer的名字不能随便改,就是找的request header的referer;如果请求中没有referer,前台会报400异常,需要捕获;后台会有org.springframework.web.bind.ServletRequestBindingException: Missing request header ‘referer3‘ for method parameter of type String可以使用request.getHeader("Referer");或者request.getHeader("referer");这样可以进行null判断。
时间: 2024-10-01 19:53:12