spring mvc controller中獲取request head內容:web
@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判斷。
在桌面建一個文件,用<a>的方式進行文件的打開是沒有referer的;一樣的文件,放到tomcat服務器裏面,就有referer了;