首页 > 其他分享 >在Struts2获得WebApplicationContext的方法

在Struts2获得WebApplicationContext的方法

时间:2023-03-20 21:01:42浏览次数:29  
标签:WebApplicationContext getSession setServletRequest request getServletContext Str


1. Action实现implements ServletRequestAware,并实现setServletRequest()方法。

public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.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);

标签:WebApplicationContext,getSession,setServletRequest,request,getServletContext,Str
From: https://blog.51cto.com/u_3871599/6138626

相关文章