CNET中国旗舰网站

ZDNet China | CNET科技资讯网 | 政府采购 | 行业网站联盟





 
标题: [求助] struts2 spring2.5 hibernate 整合 this.getHibernateTemplate() 为空
MANGO02
灵玉会员
Rank: 8Rank: 8


UID 307619
精华 1
积分 6397
帖子 649
威望 2908
ZD币 898 元
阅读权限 180
注册 2008-3-14
状态 离线
  楼主
发表于 2008-6-18 16:40  资料  个人空间  短消息  加为好友 
开发者在线

struts2 spring2.5 hibernate 整合 this.getHibernateTemplate() 为空

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"
xmlns:c
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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName">

<value>com.mysql.jdbc.Driver </value>

</property>

<property name="url">

<value>jdbc:mysql://localhost:3306/htwebsite </value>

</property>

<property name="username">

<value>root </value>

</property>

<property name="password">

<value>root </value>

</property>

</bean>


<!-- hibernate3 session Bean-->

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource"> <!-- 注入数据源 -->

<ref local="dataSource" />

</property>


<property name="mappingResources">
        <list>
            <value>test/Logintable.hbm.xml </value>
        </list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>

<prop key="hibernate.show_sql">true </prop>

</props>

</property>

</bean>
<!-- 事务管理 -->

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory">

<ref local="sessionFactory" />

</property>


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

<!--Dao Configuration-->
<bean id="LogintableDao" class="test.LogintableDao">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
<property name="hibernateTemplate">
                        <ref local="hibernateTemplate"/>
                </property>
</bean>

<bean name="adduser" class="action.AddUserAction">

</bean>
</beans>


web.config


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
test </display-name>
<servlet>
<servlet-name>log4j </servlet-name>
<servlet-class>com.apache.jakarta.log4j.Log4jInit </servlet-class>
<init-param>
<param-name>log4j </param-name>
<param-value>/WEB-INF/log4j.properties </param-value>
</init-param>
<load-on-startup>1 </load-on-startup>
</servlet>

<welcome-file-list>
<welcome-file>index.html </welcome-file>
<welcome-file>index.htm </welcome-file>
<welcome-file>index.jsp </welcome-file>
<welcome-file>default.html </welcome-file>
<welcome-file>default.htm </welcome-file>
<welcome-file>default.jsp </welcome-file>
</welcome-file-list>
<filter>
  <filter-name>strut2 </filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
</filter>
<filter-mapping>
  <filter-name>strut2 </filter-name>
  <url-pattern>/* </url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>


</web-app>

调用的地方
package test;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class LogintableDao extends HibernateDaoSupport {

//插入数据

    public void insertUser(Logintable user) {
    HibernateTemplate template = this.getHibernateTemplate();
        if(template==null)
        {
        System.out.print("null");
        }
        else
        {
        System.out.print("not null");
        template.save(user);
        }
      
    }

}




顶部
 



当前时区 GMT+8, 现在时间是 2009-7-5 06:02

  Powered by Discuz! 5.5.0 © 2001-2007 Comsenz Inc.
Processed in 0.061870 second(s), 4/3 queries

清除 Cookies - 联系我们 - ZDNetChina中文社区 - 无图版