日期:2014-05-18  浏览次数:20737 次

Struts2与Spring2配合使用如何获取ApplicationContext
各位好,
  请问在Struts2中如何获取Spring2的ApplicationContext对象,谢谢。

------解决方案--------------------
ActionContext ctx = ActionContext.getContext();
/**
* 取得spring的上下文环境
*
* @param session
* @return
*/
private WebApplicationContext getWebApplicationContext(ActionContext context) {
return WebApplicationContextUtils
.getWebApplicationContext(getServletContext(context));
}
------解决方案--------------------
漏掉一个方法
/**
* 取得ServletContext

* @param context
* @return
*/
private ServletContext getServletContext(ActionContext context) {
return (ServletContext) context.get(SERVLET_CONTEXT);
}