almost 3 years ago

在使用spring時,經常需要在普通類中獲取session,request等對象

比如一些AOP攔截器類,在有使用struts2時,因為struts2有一個接口使用org.apache.struts2.ServletActionContext即可很方便的取到session對像.
用法:ServletActionContext.getRequest().getSession();
但在單獨使用spring時如何在普通類中獲取session,reuqest呢?
其實也是有辦法的.
首先要在web.xml增加如下代碼:

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

接著在普通bean類中:

@Autowired
private HttpSession session;

@Autowired
private HttpServletRequest request;

即可,在類中使用session對像了,是不是很方便呢..
之所以要寫出來是因為目前網上關於這個的用法,都是用什麼寫個lister再把session保存起來,太麻煩了.
spring這麼強大的框架,當然他們早也想到了.所以才有了我們這麼方便的使用方法.

當前加了上面的listener後也可以使用代碼的方式獲取reuqest對像

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

如果是在 Controller 並印出所有 Header

@RequestMapping(value = "coffeebeans")
public ModelAndView index(HttpServletRequest request) {
    for (Enumeration names = request.getHeaderNames(); names.hasMoreElements(); ) {
        String name = (String) names.nextElement();
        for (Enumeration values = request.getHeaders(name); values.hasMoreElements(); ) {
            String value = (String) values.nextElement();
            System.out.println(name + ": " + value);
            //headers.add(name, value);

        }
    }
    ModelAndView mav = null;
    ShopProduct shopProduct = shopProductService.findOne(id);
    if (shopProduct != null) {
        mav = new ModelAndView("coffeebeans");
        mav.addObject("coffeebean", shopProduct);
    }
    return mav;
}

單獨抓 user-agent

@RequestHeader(value = "User-Agent") String userAgent

常見的 Header 內容如下

三星 Galaxy J7

#三星瀏覽器
host: 192.168.1.11:8080
connection: keep-alive
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-J710GN Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36
accept-encoding: gzip, deflate, sdch
accept-language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4

#Chrome
host: 192.168.1.11:8080
connection: keep-alive
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 6.0.1; SM-J710GN Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-encoding: gzip, deflate, sdch
accept-language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4

三星 Galaxy Note 8.0

#三星瀏覽器
host: 192.168.1.11:8080
connection: keep-alive
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (Linux; U; Android 4.4.2; zh-tw; GT-N5100 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30
accept-encoding: gzip,deflate
accept-language: zh-TW, en-US
accept-charset: utf-8, iso-8859-1, utf-16, *;q=0.7

#Chrome
host: 192.168.1.11:8080
connection: keep-alive
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 4.4.2; GT-N5100 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-encoding: gzip, deflate, sdch
accept-language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4

iPad

#Safari
host: 192.168.1.11:8080
connection: keep-alive
upgrade-insecure-requests: 1
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (iPad; CPU OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/14D27 Safari/602.1
referer: http://192.168.1.11:8080/
accept-language: zh-tw
accept-encoding: gzip, deflate

#Chrome
host: 192.168.1.11:8080
upgrade-insecure-requests: 1
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (iPad; CPU OS 10_2_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.79 Mobile/14D27 Safari/602.1
accept-language: zh-tw
accept-encoding: gzip, deflate
connection: keep-alive
← Spark teaching (2) -Spark1.2 installation 創建React Native專案 →
 
comments powered by Disqus