框架:spring+Struts2
尝试1:
protected Map getSession() throws Exception {
return ActionContext.getContext().getSession();
}
结果:ActionContext空指针异常;
尝试2:
public static HttpServletRequest getRequest(){
HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return req;
}
结果:失败,RequestContextHolder空指针;
尝试3:
//因quartz执行的方法 拿不到request和session 固写该方法
protected ServletContext getServletContext() {
return ContextLoader.getCurrentWebApplicationContext().getServletContext();
}
protected void servletContextSetAttribute(String key, Object value) {
ServletContext sc = ContextLoader.getCurrentWebApplicationContext().getServletContext();
sc.setAttribute(key, value);
}
protected Object servletContextGetAttribute(String key) {
ServletContext sc = ContextLoader.getCurrentWebApplicationContext().getServletContext();
return sc.getAttribute(key);
}
结果:成功;
通过 ServletContext ,在进行存储全局的值;