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

boytomato 2008-06-18 03:57:06
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:context="http://www.springframework.org/schema/context"
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/schema/context/spring-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);
}

}

}


...全文
340 9 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Landor2004 2008-06-19
你这个applicationContext.xml放在什么目录下了
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
似乎默认的是WEB-INF目录
还有
<!-- 事务管理 -->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
这个你也没挂事务啊,可以看看spring的中文文档找找spring配置文件怎么写吧
还有后台有没有什么错误提示什么的

  • 打赏
  • 举报
回复
boytomato 2008-06-18
改后仍然为空。。。。
  • 打赏
  • 举报
回复
boytomato 2008-06-18
<?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:context="http://www.springframework.org/schema/context"
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/schema/context/spring-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="LogintableDao" class="test.LogintableDao">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean name="adduser" class="action.AddUserAction">

</bean>
</beans>
  • 打赏
  • 举报
回复
rayt_1982 2008-06-18
<bean id="LogintableDao" class="test.LogintableDao">
<property name="hibernateTemplate">
<ref local="hibernateTemplate"/>
</property>
</bean>
  • 打赏
  • 举报
回复
Landor2004 2008-06-18
另外提一下,既然你用了spring2.5了,那么为什么不用anotation,返回还要在xml里面配置LogintableDao
  • 打赏
  • 举报
回复
Landor2004 2008-06-18
<property name="hibernateTemplate">
<ref local="hibernateTemplate"/>
</property>


这个也不需要了,都去掉了,试试
  • 打赏
  • 举报
回复
Landor2004 2008-06-18
你都继承HibernateDaoSupport 了,直接注入sessionFactory就行了,不用再写
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
  • 打赏
  • 举报
回复
相关推荐
发帖
Java EE
加入

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2008-06-18 03:57
社区公告
暂无公告