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

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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题