session的获取:ActionContext.getContext().getSession()
servlet的获取 ServletActionContext
action上下文:ActionContext
通过ActionContext可以获得:
ActionContext ctx = ActionContext.getContext(); //Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise. Map<String, Object> smap = ctx.getApplication(); //struts2的context Map<String, Object> s2map = ctx.getContextMap(); //获取原生的HttpServletRequest HttpServletRequest request = (HttpServletRequest) ctx.get(StrutsStatics.HTTP_REQUEST); //获取请求参数 Map<String, Object> pmap = ctx.getParameters(); //Session Map<String, Object> session = ctx.getSession();
如果是个通过spring实现的ioc应用,则可以获得spring的上下文:
WebApplicationContext context = (WebApplicationContext) smap.get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); DataSource dateSource = (DataSource) context.getBean("dataSource");