caojing000 2010-03-20 00:11
浏览 319
已采纳

ssh整合getHibernateTemplate().find()报空指针异常

[color=darkred][/color]
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
        value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
    </property>
    <property name="url"
        value="jdbc:microsoft:sqlserver://localhost:1433;databasename=zf">
    </property>
    <property name="username" value="sa"></property>
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.SQLServerDialect
            </prop>
            <prop key="hibernate.show_sql">
                true
            </prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/chen/model/TblFwlx.hbm.xml</value>
            <value>com/chen/model/TblFwxx.hbm.xml</value>
            <value>com/chen/model/TblJd.hbm.xml</value>
            <value>com/chen/model/TblQx.hbm.xml</value>
            <value>com/chen/model/TblUser.hbm.xml</value></list>
    </property></bean>

                   <bean name="FwxxDao" class="com.cao.dao.FwxxDao"/>
                <bean name="LoginDao" class="com.cao.dao.LoginDao"/>

applicationcontext-aop.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName" default-lazy-init="true">

<aop:config proxy-target-class="true">
    <aop:advisor
        pointcut="execution(* com.cao.dao.*Dao.*(..))"
        advice-ref="txAdvice" />
</aop:config>


<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*"  propagation="REQUIRED" read-only="true"/>
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="*"  propagation="REQUIRED" read-only="true"/>
    </tx:attributes>
</tx:advice>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

FwxxAction代码:

public class FwxxAction extends DispatchAction {
private FwxxDao fwxxDao;

public FwxxDao getFwxxDao() {
return fwxxDao;
}
public void setFwxxDao(FwxxDao fwxxDao) {
this.fwxxDao = fwxxDao;
}
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return doList(mapping, form, request, response);
}
public ActionForward doList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("in..........doList");
System.out.println("fwxxDao======="+fwxxDao);
int index=1;
int pageSize=5;
try{
String str=request.getParameter("index");
index=Integer.parseInt(str);

    }catch(Exception e){

    }
    List list=fwxxDao.getAll();
    request.setAttribute("list", list);
    return mapping.findForward("fwxxList");

}

}

Dao代码:

package com.cao.dao;

import java.util.List;

import com.cao.model.tblFwxx;

public class FwxxDao extends BaseDao {

public FwxxDao() {
    super.cls=tblFwxx.class;
}

public List getFwxx(){
    return super.getAll();  
}
}

BaseDao代码:

public class BaseDao extends HibernateDaoSupport {

protected Class cls;

public List<T> getAll(){

    return getHibernateTemplate().findByExample(cls);
}

}

struts-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">







attribute="fwxxForm"
name="fwxxForm"
parameter="method"
path="/fwxx"
scope="request"
type="com.cao.struts.action.FwxxAction" >





action-servlet.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="byName">

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>


contextConfigLocation
classpath:applicationContext-*.xml

<!-- 著名 Character Encoding filter -->

encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8

<!--Hibernate Open Session in View Filter控制Session的连接-->
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>


org.springframework.web.context.ContextLoaderListener


action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml


debug
3


detail
3

0


action
*.do


index.jsp

最后运行,getHibernateTemplate()为空,调用它的find方法抛空指针异常

用ApplicationContext con=new ClassPathXmlApplicationContext("applicationContext-*.xml");
con.getBean("FwxxDao");
可以得到对象并可以调它的方法

  • 写回答

3条回答 默认 最新

  • qwe_rt 2010-03-20 10:03
    关注

    [code="java"]public void setFwxxDao(FwxxDao fwxxDao) {
    this.fwxxDao = fwxxDao;
    } [/code]
    [code="java"]default-autowire="byName"[/code]
    byName的name是java中setXxxx 的Xxxx。
    举个例子:
    [code="java"]public class Service
    {
    Source source1;

    public void setSource(Source source1)
    {
        this.source1 = source1;
    }
    

    }[/code]
    applicationContext.xml为
    [code="xml"]<beans

    ...

    default-autowire="byName">

    <bean id="source" class="cn.hh.spring.DBCPSource" scope="prototype"/>
       </bean>
    


    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面