ssh项目在运行时控制台没有出错,但是页面出现
HTTP Status 500 - No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here怎么解决???
applicationContext.xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
<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.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>vo/News.hbm.xml</value></list>
</property></bean>
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
tx:attributes
<!-- 只有一save、delete、update开头的方法才能执行增删改操作 -->
<!-- 其他方法为只读方法 -->
/tx:attributes
/tx:advice
aop:config
<!-- 对应实现类接口的包的位置 -->
/aop:config
<!-- 业务层Service -->
<bean id="news_service" class="service.impl.NewsServiceImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<!-- 控制层Action -->
<bean id="news_action" class="action.NewsAction">
<property name="news_services">
<ref bean="news_service" />
</property>
</bean>
</beans>
初学ssh的菜鸟,请各位大神帮忙解答一下!!谢谢!
出现这个情况是对新版本的hibernate框架了解不熟悉导致的,新版本的hibernate要求开发者必须配置事务管理,因为session的创建和销毁都是被事务管理的。出现则种情况,是你的事务管理没有配置好
在appliactionContext.xml中配置如下:
<!-- 配置事务管理器 -->
然后在需要配置事务的方方法上添加@Transaction注解。注意两点:HibernateTransactionManager要对应你所用的hibernate版本;2、Transaction要用spring库里的。
在appliactionContext.xml中配置如下:
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
然后在需要配置事务的方方法上添加@Transaction注解。注意两点:HibernateTransactionManager要对应你所用的hibernate版本;2、Transaction要用spring库里的。