0 0

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

[color=darkred][/color]
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
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"/>

</beans>


applicationcontext-aop.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 以AspectJ方式 定义 AOP -->
<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>

</beans>


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<tblFwxx> {


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

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


BaseDao代码:

public class BaseDao<T> 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">

<struts-config>
  <data-sources />
  <form-beans >
      <form-bean name="fwxxForm" type="com.cao.struts.form.FwxxForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards ></global-forwards>
  <action-mappings >
        <action
      attribute="fwxxForm"
      name="fwxxForm"
      parameter="method"
      path="/fwxx"
      scope="request"
      type="com.cao.struts.action.FwxxAction" >
      <forward name="fwxxList" path="/WEB-INF/jsp/FwxxList.jsp"></forward>
      </action>

  </action-mappings>
  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
  <message-resources parameter="com.chen.struts.ApplicationResources" />
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>
</struts-config>


action-servlet.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans
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">
<bean name="/fwxx" class="com.cao.struts.action.FwxxAction"/>
</beans>


web.xml代码:



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>

 
  <!-- 著名 Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<!--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>


  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


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


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

问题补充:那个是路径问题,怎么能改小写呢这里也不需要注入啊,配置文件头部都有default-autowire="byName",ApplicationContext忘记写了~~添上去还是一样

问题补充:是Dao的问题,要改小写,但不要注入sessionFactory,这样就OK了,谢谢了…
2010年3月20日 00:11

3个答案 按时间排序 按投票排序

0 0

采纳的答案

public void setFwxxDao(FwxxDao fwxxDao) { 
this.fwxxDao = fwxxDao; 
} 

default-autowire="byName"

byName的name是java中setXxxx 的Xxxx。
举个例子:
public class Service
{
    Source source1;

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

}

applicationContext.xml为
<beans 


   ...

   default-autowire="byName">
    
    <bean id="source" class="cn.hh.spring.DBCPSource" scope="prototype"/>
       </bean>
</beans>

2010年3月20日 10:03
0 0

修改 applicationContext.xml文件中的fwxxDao bean

<?xml version="1.0" encoding="UTF-8"?> 
<beans 
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"> //fwxxDao首字母小写 
<property name="sessionFactory" ref="sessionFactory"/> //添加sessionFactory 
</bean> 
<bean name="loginDao" class="com.cao.dao.LoginDao"/>//loginDao小写 
<property name="sessionFactory" ref="sessionFactory"/>//添加... 
</beans> 

2010年3月20日 01:12
0 0

修改 applicationContext.xml文件中的fwxxDao bean
<?xml version="1.0" encoding="UTF-8"?>
<beans
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"> //fwxxDao首字母小写
<property name="sessionFactory" ref="sessionFactory"/> //添加sessionFactory
</bean>
<bean name="loginDao" class="com.cao.dao.LoginDao"/>//loginDao小写
<property name="sessionFactory" ref="sessionFactory"/>//添加...
</beans>

2010年3月20日 01:11

相关推荐

    gethibernatetemplate的find方法

    gethibernatetemplate的find方法,find(String queryString);find(String queryString , Object value);find(String queryString, Object[] values);findByExample(Object exampleEntity);findByExample(Object ...

    jsp中调用dao的getHibernateTemplate()时,报空指针

    jsp中调用dao的getHibernateTemplate()时,报空指针

    hibernate模板类详解

    find(String queryString , Object value); 示例:this.getHibernateTemplate().find("from bean.User u where u.name=?", "test"); 这样的例子里面还有很多

    getHibernateTemplate()有模糊查询和 分页

    getHibernateTemplate()有模糊查询和 分页getHibernateTemplate()有模糊查询和 分页

    getHibernateTemplate()查询

    getHibernateTemplate()查询 hibernate hql

    getHibernateTemplate()使用方法

    整合了hibernate的getHibernateTemplate()使用方法 全面详细 带有相应的例子

    getHibernateTemplate

    getHibernateTemplate()方法

    ssh(structs,spring,hibernate)框架中的上传下载

     本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0  本文选用的数据库为Oracle 9i,当然你可以在不...

    getHibernateTemplate分页-模糊查询

    getHibernateTemplate分页-模糊查询.hibernate查询以及分页查询等方法使用。

    Struts2 Spring hibernate 国际化 表单验证 安全退出

    这是之前分页的升级版 带国际化 表单验证 用户登录 安全退出 功能都实现了... List list= this.getHibernateTemplate().find("from Students where username=? and password=?", param); return list.isEmpty(); }

    手机充值卡充值

    return (Card)this.getHibernateTemplate().get(Card.class, cardPass); } @Override public void updateCard(Card card) { // TODO Auto-generated method stub this.getHibernateTemplate().update(card...

    spring 声明式事务

    1.本例子的使用了 ssh 框架 2.本例子DAO层 使用了 getHibernateTemplate 来实现数据的新增修改和删除 3.本例子使用了声明式事务 4.本例子提供了详细的使用方法,可以根据 readme.txt 来逐步的验证声明式事务是否起...

    SSH2增删改查使用HibernateTemplate

    SSH2增删改查使用HibernateTemplate

    图书查询-Java-Web程序设计教程-[共2页].pdf

    在 DAO 实现类 BookDAO.java 代码如下: public List getRequiredBookByName(String name){ try{ List books=this.getHibernateTemplate(). find("from Book where bookname like '%"+name+"%'"); return books; }...

    第24次课-1 Spring与Hibernate的整合

    为了更好地与持久层框架整合,Spring还提供了统一的异常处理体系和事务管理方法。 24.1 概述 24.1.1 概述 如果Spring与Hibernate进行了整合,则Hibernate便处于被Spring管理的状态下,Hibernate所需的基础资源,都由...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    SSH框架(web端+json手机端)

    基于S2HH的框架,里面带有数据库,另外分为web端和手机端,手机端才有json格式传递数据。里面包括了文件上传,各种数据查询,数据库操作使用的是getHibernateTemplate()

    三大框架下分页源代码

    log.debug("find TblNewsclass instance"); try { String sql = "select new TblNews(id,title,pubdate,status) from TblNews"; Query query = getHibernateTemplate().getSessionFactory()....

    java:递归取出List中的元素

    List ls = dao.getHibernateTemplate().find(hql); Iterator it = ls.iterator(); if(it.hasNext()){ // while 用这个的时候和下面的一样 Object[] obj = (Object[]) it.next(); ...

    客户关系管理系统框架搭建(二)

    --3 配置本地化代理工程bean,这是spring整合hibernate的入口 --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;!--表示在类路径下加载...

Global site tag (gtag.js) - Google Analytics