在Struts2获得WebApplicationContext的方法
1. Action实现implements ServletRequestAware,并实现setServletRequest()方法。
public void setServletRequest(HttpServletRequest request) {// TODO Auto-generated method stubthis.request = request;}
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());//失败时抛出异常 ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); //失败时返回null
WebApplicationContext webApplicationContext = (WebApplicationContext)request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);