日期:2014-05-18  浏览次数:20798 次

高手进来看看啊
代码如下
public   class   SpringBaseBeanAction   extends   ActionSupport{

  String   beanName= " ";
  public   SpringBaseBeanAction(){
 
  }
 
  public   SpringBaseBeanAction(String   beanName){
  this.beanName=beanName;
  }
 
public     Object   getBean(String   beanName){
WebApplicationContext   webapp=getWebApplicationContext();
Object   obj=webapp.getBean(beanName);
return   obj;
}

public     Object   getBean(){
WebApplicationContext   webapp=getWebApplicationContext();

return   webapp.getBean(beanName);
}


}
以上是调用getBean(String   beanName)得到类的实例


package   com.txForum.dao;

import   net.sf.hibernate.Query;

import   org.apache.commons.logging.Log;
import   org.apache.commons.logging.LogFactory;
import   org.hibernate.HibernateException;
import   org.hibernate.Session;
import   org.springframework.context.ApplicationContext;
import   org.springframework.context.support.FileSystemXmlApplicationContext;
import   org.springframework.orm.hibernate3.HibernateCallback;
import   org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import   java.util.*;
public   class   CommonUserDao   extends   HibernateDaoSupport   {

private   static   final   Log   log   =   LogFactory.getLog(CommonUserDao.class);

protected   void   initDao()   {
//do   nothing
}


public   List   ExecQueries(final   String   sql){
return   (List)getHibernateTemplate().execute(new   HibernateCallback(){
public   List   doInHibernate(Session   session)throws   HibernateException{
List   result=session.createQuery(sql).list();
return   result;
}
});
 

}

}

在spring   的appilcationContext.xml里CommonUserDao类配置如下

<bean   id= "CommonUserDao "
class= "com.txForum.dao.CommonUserDao ">
<property   name= "sessionFactory ">
<ref   bean= "TXFSessionFactory "   />
</property>
</bean>

问题就是在jsp页面中始终都得不到该对对像的实例
<%@   page   language= "java "   pageEncoding= "UTF-8 "%>

<%@   page   import= "com.txForum.dao.* "   %>  
<%@   page   import= "com.txForum.struts.action.* "   %>
<%

SpringBaseBeanAction   s=new   SpringBaseBeanAction();
CommonUserDao   c=(CommonUserDao)s.getBean( "CommonUserDao ");

  %>

以下是报错内容
java.lang.NullPointerException
com.txForum.struts.action.SpringBaseBeanAction.getBean(SpringBaseBeanAction.java:20)
org.apache.jsp.w