Struts Spring Hibernate 框架整合时候出现的问题

1.Tomcat和项目的lib目录下均要加入mysql数据库驱动mysql-connector-java-5.0.8-bin.jar。在bean中配置数据源的时候,请注意严格按照下面方式配置:

<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
 <property name="url" value="jdbc:mysql://localhost:3306/drdatabase"></property>
 <property name="username" value="root"></property>
 <property name="password" value="123456"></property>
 <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>

 上面的黑色部分我曾经出错了,导致Cannot Load JDBC 异常。

 

2. Struts action 配置与Spring配置时候出现的问题:

 <?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <package name="doctors" extends="struts-default">  
         <action name ="savePatient" class="savePatientAction">
            <result  name="redirect">ListPatient.action</result>
            <result name="input">/AddPatients.jsp</result>
        </action>
 </package>

</struts>

 

name的名称与显示层的JSP的Form表单提交名称一致,class的名称必须与在Spring中配置的bean的id号一致:

<bean id="savePatientAction" class="com.chenx.action.SavePatientAction">
    <property name="patientService">
    <ref bean="patientService"/>
    </property>
</bean>

 

3.

There is no Action mapped for namespace / and action name login
首先出现这种错误是因为配置文件没找到,检查一下你的struts.xml是否在classes 文件夹下
路径:“安装Tomcat 的路径”\webapps\“你工程的名称”\WEB-INF\classes
其次,查看你的struts.xml这个文件名称是否与“struts.xml”一样,并且检查其中内容 是否正确,每一个属性都要检查
注意拼写错误!!!!!!
再次,如果没用<s:form>的话 形式应该是这样:<form  action="strut/login.action" >  
如果你用的是<s:form >形式 那应该是:<s:form name ="form1" action ="login" namespace="/strut">
↓↓↓要记住↓↓↓
form 标签  没有namespace属性
s:form 标签  有namespace属性
 
4. 框架整合时候asm.jar 以及 asm.2.0.6.jar均可以删除,Spring和Hibernate的冲突带来的。还有xerecs也可以删除。
 
5.异常1:not-null property references a null or transient value
解决方法:将“一对多”关系中的“一”方,not-null设置为false
(参考资料:http://www.thearcmind.com/confluence/pages/viewpage.action?pageId=212)

异常2:org.hibernate.TransientObjectException: object references an unsaved transient instance
解决方法:cascade="save-update,persist"
(参考资料:http://www.laliluna.de/254.html)

异常3:org.hibernate.QueryException: could not resolve property
解决方法:"from Category category where category.userID = :userID"修改为"from Category category where userID = :userID"或者"from Category category where category.user.id = :userID"
(参考资料:http://www.laliluna.de/277.html)

异常4:could not initialize proxy - the owning Session was closed
解决方法:设置lazyfalse
(参考资料:http://forum.springframework.org/showthread.php?t=27993
 
6.Spring 中的bean默认为单例模式,type=“singleton”,在消息映射或是登陆的时候就会出现各种各样的异常,比如我设置成功的hibernate后台打印的消息并不是请求一次就打印一次,而是有丢失。
因此所有的bean必须为多态模式。切记!
<bean id="LoginAction" class="com.chenx.action.LoginAction" scope="prototype">
    <property name="userService">
    <ref bean="userService"/>
    </property>
</bean>
 

posted on 2011-09-01 09:35  追求卓越 挑战极限  阅读(187)  评论(0编辑  收藏  举报

导航