我在spring中配置的事务:
         <!-- sessionfactor实例 --> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- hibernateTemplate实例 -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="targetMethod"
expression="execution(* com.ite.*.action.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" />
</aop:config>  
         <!-- 注入服务对象-->
<bean id="userService" class="com.ite.users.service.UserService">
<property name="usersDAO">
<ref bean="UsersDAO" />
</property>
</bean> <bean id="UsersDAO" class="com.ite.users.dao.UsersDAO">
<property name="template">
<ref bean="hibernateTemplate" />
</property>
</bean> <!-- 注入action -->
<bean name="/users" class="com.ite.users.action.UsersAction">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
另外struts配置的这个action:
<action attribute="usersForm" parameter="method"
name="usersForm" path="/users"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="index" path="/index.jsp"></forward>
</action>

然后在页面中:
                  <html:form action="/users?method=login">
<html:text property="name"></html:text>
<html:text property="password"></html:text>
<html:submit />
<html:cancel />
</html:form>
提交后执行public class com.ite.users.action.UsersAction extends DispatchAction 中的
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UsersForm usersForm = (UsersForm) form;// TODO Auto-generated method stub
Users u = new Users();
u.setName(usersForm.getName());
u.setPassword(usersForm.getPassword());
boolean result =  userService.login(u);
request.setAttribute("user", u);
request.setAttribute("result", result+"");
return mapping.findForward("index");
}
这个方法,报以下错误:
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here查了一下,网上说是没有配置事务,是不是我配置错误呢?请各位大侠帮忙解决下!

解决方案 »

  1.   

    1:查看expression="execution(* com.ite.*.action.*.*(..))" /> 是否写错
    2:查看是否引入相应的DTD
      

  2.   

    expression="execution(* com.ite.*.action.*.*(..))" /> 事务配到action层了吧,这个应该配到业务层去,不知道struts1可以配到action层不,反正struts2是不可以的。
      

  3.   

    配到业务层报错:
     Error creating bean with name '/users' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
    PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy4] to required type [com.ite.users.service.UserService] for property 'userService'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy4] to required type [com.ite.users.service.UserService] for property 'userService': no matching editors or conversion strategy found
      

  4.   

    这个表达式小弟也是第一次写,spring用的是2.0的,不知道是不是这两个dtd呢?
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    整个的文件头如下:
    <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"
    xmlns:context="http://www.springframework.org/schema/context"
    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   
    ">
      

  5.   

    另外我的数据源配置是在hibernate里,不在spring里,是不是跟这个有关系?