struts和springmvc相关获取ServletContext()方式

本文探讨了在Struts和Spring MVC中如何获取ServletContext,包括ServletActionContext和Spring容器中的WebApplicationContext。此外,还介绍了在不同加载方式下,如servlet和listener方式,如何在web.xml配置中取得Spring管理的bean。推荐了一种通用方法,通过SpringContextHolder静态类获取Bean。
摘要由CSDN通过智能技术生成

1 ServletContext和ServletActionContext,ActionContext有什么区别

 servletContext提供了标准的Servlet运行环境,其实就是一些servlet和web 容器进行通信的方法。
 ServletActionContext 其实是ActionContext的子类,其功能脱胎于ActionContext,对ActionContext的方法做了一定的包装,提供了更简便直观的方法。
 ActionContext来源于Struts2,ActionContext就是为了弥补strtus2 action跳出标准servlet框架而造成的和WEB环境失去联系的缺陷。

2 struts和springmvc都可以获得servletContext

2.1 struts获得servletContext:

                  ServletActionContext.getServletContext()

2.2 springmvc获得servletContext(应该属于spring获得)

方式一:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();  
        ServletContext servletContext = webApplicationContext.getServletContext();
方式二:
public void aa( HttpServletRequest request ){
ServletContext servletContext=request.getSession().getServletContext(); 
}

2.3 servletContext获得WebApplicationContext

方式一:
WebApplicationContext webApplicationContext = 
			(WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
方式二:
WebApplicationContext webApplicationContext 
					=WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); 

其中 方式二:
servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 


举例:
import org.springframework.web.context.support.WebApplicationContextUtils;
public void aa( HttpServletRequest request ){


//    方式一
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
 //   方式二
    WebApplicationContext webApplicationContext = (WebApplicationContext)request.getSession().getServletContext()
				.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		
}

3 在struts中获得服务器下文件路径:

ServletActionContext.getServletContext().getRealPath("/文件名")

4 springmvc常用,在spring容器中 获得request

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
//      页面请求路径
        String URL = request.getRequestURL().toString();

5 在struts中应用,转自http://blog.csdn.net/hxj135812/article/details/50127811

package cn.itcast.oa.web.listener;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.itcast.oa.domain.Privilege;
import cn.itcast.oa.service.IPrivilegeService;
import cn.itcast.oa.service.impl.PrivilegeServiceImpl;

/**
 * OA项目初始化监听器
 * @author <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">V-HUXJ</span>
 * 
 */
public class OAInitListener implements ServletContextListener {
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("OA初始化的监听器执行了。。。");
		// 获取spring工厂对象
		WebApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(sce.getServletContext());
		// 从spring工厂中获取一个PrivilegeService对象
		IPrivilegeService priService = (IPrivilegeService) context
				.getBean("privilegeServiceImpl");
		// 使用Service对象查询权限菜单数据
		List<Privilege> priTopList = priService.findTopList();
		sce.getServletContext().setAttribute("priTopList", priTopList);
		
		//查询系统中所有的权限对应的url
		List<String> urls = priService.findAllUrls();
		sce.getServletContext().setAttribute("urls", urls);
	}

	public void contextDestroyed(ServletContextEvent arg0) {
	}
}

List<String> urls = (List<String>) ServletActionContext
					.getServletContext().getAttribute("urls");
			// 判断当请求的url是否需要校验
			if (urls.contains(url)) {// 进行权限校验
				boolean b = user.checkPrivilegeByUrl(url);
				if (b) {// 如果校验通过,放行
					String invoke = invocation.invoke();// 放行
					return invoke;
				} else {// 如果校验失败,跳转到没有权限的提示页面
					return "noPrivilegeUI";
				}

6 如何取得Spring管理的bean (请用第3种方法)转自:http://elf8848.iteye.com/blog/875830

1、servlet方式加载时,
【web.xml】

Xml代码  收藏代码
<servlet>  
<servlet-name>springMVC</servlet-name>  
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
<init-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>classpath*:/springMVC.xml</param-value>  
</init-param>  
<load-on-startup>1</load-on-startup>  
</servlet> 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菠萝科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值