struts和springmvc相关获取ServletContext()方式
来源:互联网 发布:mysql中text最大长度 编辑:程序博客网 时间:2024/05/06 02:06
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);//查询系统中所有的权限对应的urlList<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>
spring容器放在ServletContext中的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC
注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。
ServletContext sc=略 WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
2、listener方式加载时:
【web.xml】
【jsp/servlet】可以这样取得
- ServletContext context = getServletContext();
- WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context);
3、通用的方法来了,神器啊,前的 1、2两种方法并不通用,可以抛弃了。
在配置文件中加入:
- <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 -->
- <bean class="com.xxxxx.SpringContextHolder" lazy-init="false" />
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- /**
- * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
- *
- */
- public class SpringContextHolder implements ApplicationContextAware {
- private static ApplicationContext applicationContext;
- /**
- * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
- */
- public void setApplicationContext(ApplicationContext applicationContext) {
- SpringContextHolder.applicationContext = applicationContext; // NOSONAR
- }
- /**
- * 取得存储在静态变量中的ApplicationContext.
- */
- public static ApplicationContext getApplicationContext() {
- checkApplicationContext();
- return applicationContext;
- }
- /**
- * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
- */
- @SuppressWarnings("unchecked")
- public static <T> T getBean(String name) {
- checkApplicationContext();
- return (T) applicationContext.getBean(name);
- }
- /**
- * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
- */
- @SuppressWarnings("unchecked")
- public static <T> T getBean(Class<T> clazz) {
- checkApplicationContext();
- return (T) applicationContext.getBeansOfType(clazz);
- }
- /**
- * 清除applicationContext静态变量.
- */
- public static void cleanApplicationContext() {
- applicationContext = null;
- }
- private static void checkApplicationContext() {
- if (applicationContext == null) {
- throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
- }
- }
- }
转载标明链接:http://blog.csdn.net/wabiaozia/article/details/51578014
我博客所有文章目录:http://blog.csdn.net/wabiaozia?viewmode=contents
本文参考:spring3.x和http://www.blogjava.net/Todd/archive/2010/04/22/295112.html , http://guomingzhang2008.iteye.com/blog/1739294
- struts和springmvc相关获取ServletContext()方式
- 获取servletContext springMvc获取servletContext
- 获取servletContext对象的方式
- Struts获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象
- Servlet和ServletContext相关笔记
- 【JSP开发】ServletContext的获取方式
- 获取servletContext对象的两种方式
- struts和springmvc
- springMVC和Struts异同
- SpringMVC和Spring struts
- 对比Struts和SpringMVC
- Struts 2获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象
- Struts 获取HttpServletRequest和HttpServletResponse的两种方式
- springMVC 中几种获取request和response的方式
- springMVC 中几种获取request和response的方式
- springMVC 中几种获取request和response的方式
- springMVC 中几种获取request和response的方式
- springMVC 中几种获取request和response的方式
- 菜鸟成长记-仿今日头条雏形
- 【MyBatis框架点滴】——MyBatis一对一查询
- Tachyon:Spark生态系统中的分布式内存文件系统
- 排序算法
- 【Leetcode】Insert Interval
- struts和springmvc相关获取ServletContext()方式
- Mockplus
- HP电脑 VMware Workstation Pro装Centos6.7 报错
- JavaScript基础——面向对象的程序设计(一)创建对象的几种方式总结
- 【算法总结系列-6】深度优先搜索-Depth First Search,DFS
- 基因数据处理49之cloud-scale-bwamem运行成功
- 将Web应用性能提高十倍的10条建议
- [arcengine][栅格]保存为本地文件
- 排序算法2