biGpython

生亦何欢,死亦何苦? 予我長袖,我必善舞!

导航

Struts2中ServletActionContext.getServletContext().getRealPath("/")空指针

准备工作:xwork源码一份(我的版本是2.0.4)。

 

1.当时我是这么建立这个类的

 

Java代码  收藏代码
  1. public class StaticAction extends ActionSupport {  
  2.     static {  
  3.         logDir = ServletActionContext.getServletContext().getRealPath("/"); //在这里打个断点  
  4.     }  
  5.   
  6. }  

 

2.在com.opensymphony.xwork2.DefaultActionInvocation 类下大约第362行

 

Java代码  收藏代码
  1. private void init() throws Exception {  
  2.         Map contextMap = createContextMap();  
  3.   
  4.         createAction(contextMap); //在这个方法打个断点,该方法会去创建你所调用的Action。详情可以看看它里面的objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);这一句  
  5.   
  6.         if (pushAction) {  
  7.             stack.push(action);  
  8.         }  
  9.   
  10.         invocationContext = new ActionContext(contextMap); //注意你所调用的Action,它的ActionContext在这里才会创建。  
  11.         invocationContext.setName(proxy.getActionName());  
  12.   
  13.         // get a new List so we don't get problems with the iterator if someone changes the list  
  14.         List interceptorList = new ArrayList(proxy.getConfig().getInterceptors());  
  15.         interceptors = interceptorList.iterator();  
  16.     }  

 

 

好了。调试运行。因为StaticAction有静态代码块,而objectFactory在创建action实例时,使用的是Class.forName(String cls)方式,它的具体实现如下:

 

Java代码  收藏代码
  1. java.lang.Class.java  
  2.   
  3.     public static Class<?> forName(String className)   
  4.                 throws ClassNotFoundException {  
  5.         return forName0(className, true, ClassLoader.getCallerClassLoader());  
  6.     }  

 

 

 

看到forName0的第二个参数设置为true了吗?表示该类被JVM装载后,要不要立即初始化,如果设置成false,表示在将初始化的工作推迟到了newInstance的时候进行.所以,当方法createAction(contextMap)被调用时,要执行的StaticAction的静态代码块肯定会被调用的。


当运行到ServletActionContext.getServletContext().getRealPath("/")这一句时,继续查看ServletActionContext.getServletContext()的来源:


org.apache.struts2.ServletActionContext.java 大概第137行

 

 

Java代码  收藏代码
  1. public static ServletContext getServletContext() {  
  2.     return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT);  
  3. }  

 

可以看到其实调用的是ActionContext.getContext().get(SERVLET_CONTEXT);过来的,而咱们在最开始也看到了createAction(contextMap);方法在 invocationContext = new ActionContext(contextMap); 方法执行之前,是无法正常得到SERVLET_CONTEXT的,所以,就会出空指针异常了。

posted on 2012-03-27 21:25  biGpython  阅读(8545)  评论(0编辑  收藏  举报