首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

在Struts2取得WebApplicationContext的方法

2012-09-05 
在Struts2获得WebApplicationContext的方法1. Action实现implements ServletRequestAware,并实现setServle

在Struts2获得WebApplicationContext的方法
1. Action实现implements ServletRequestAware,并实现setServletRequest()方法。

public void setServletRequest(HttpServletRequest request) {// TODO Auto-generated method stubthis.request = request;}



以下是获得的方法:
A.
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());   ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());//失败时抛出异常   ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); //失败时返回null


B.
WebApplicationContext webApplicationContext = (WebApplicationContext)request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 

热点排行