您的位置:首页 > 其它

SSH集成中错误解决过程及从中学到的

2011-08-11 21:28 162 查看
N天前做了一个小demo,主要是学习SSH集成。Demo很小不过错误百出,在调试这些错误的时候也更深刻的理解SSH的运行机制。对于繁杂的环境的搭建也熟练掌握了。
按着SSH的环境搭建过程,粗糙的搭建起来了。拷贝jar包、配置web.xml、配置struts-confige.xml文件、配置applicationContext-*.xml文件等。编写各层的代码。然后准备测试,下面就是一系列问题的出现及解决:
1、找不到listner。
这个问题是因为在web.xml文件中我没有配置ContextLoaderListener。解决:
在web.xml文件中加入:

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


需要注意的是:这个listener要写在servlet之前。

具体为什么会在启动tomcat的时候报这个错误不记得了。不过这里说一下ContextLoaderListener的作用。
ContextLoaderListener预设会读取applicationContext.xml,您可以指定自己的定义档,只要在<context-param>中指定"contextConfigLocation"参数,例如:
...

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-config.xml</param-value>
</context-param>

在自己所定义的Servlet类别中使用Spring的容器功能,则也可以使用org.springframework.web.context.ContextLoaderListener,例如在web.xml中使用<listener>标签加以定义:

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

加入了listener,重新启动tomcat,打开IE,输入网址,第二个错误出现了:
2、页面找不到。
这个错误纯属大意造成,原因:tomcat启动报错,因此再tomcat错误启动的时候打开页面,就会报告页面找不到。Tomcat报告的错误如下:
3、UnexpectedexceptionparsingXMLdocumentfromfile[E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-common.xml];nestedexceptionisjava.lang.NoClassDefFoundError:org/aspectj/weaver/tools/PointcutPrimitive
这个错误是因为applicationContext-common.xml这个配置文件写的有误:在配置pointcut的时候
<!--那些类那些方法使用事务-->
<aop:config>
<aop:pointcutid="allManagerMethod"expression="execution(*manager.*.*(..))"/>
<aop:advisorpointcut-ref="allManagerMethod"advice-ref="txAdvice"/>
</aop:config>




expression="execution(*manager.*.*(..))这个属性写错了。

继续重启tomcat,继续错误:
4、Errorcreatingbeanwithname'/addDepartment'definedinfile[E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-actions.xml]:Cannotresolvereferencetobean'department'whilesetting
beanproperty'department';nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionException:Nobeannamed'department'isdefined
这个是applicationContext.xml文件配置有误,也就是类的注入有问题,是缺少类的注入。根据错误提示,把缺少的类加上就行了。

5、下面的错误报告很多,不过问题不大,主要是在最后一句话:sessionFactoryorhibernateTemplateisrequired。由于dao层继承HibernateSupport,将session的管理都放到了Spring中,因此,在配置applicationContext.xml文件的时候需要在注入dao层类的时候把sessionFactory作为一个属性来注入。而我没有注入,因此错误出现。由于dao层有错,manager层中的类注入dao的时候就会报错,而servlet,也就是Action又要注入manager,因此错误堆栈打出来的是先从Action的注入开始到manager,最后是dao。错误报告如下:
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'/addDepartment'definedinfile[E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-actions.xml]:
Cannotresolvereferencetobean'departmentManager'whilesettingbeanproperty'departmentManager';nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'departmentManager'definedinfile[E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-beans.xml]:
Cannotresolvereferencetobean'departmentDao'whilesettingbeanproperty'departmentDao';nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'departmentDao'definedinfile[E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-dao.xml]:
Invocationofinitmethodfailed;nestedexceptionisjava.lang.IllegalArgumentException:sessionFactoryorhibernateTemplateisrequired
Causedby:

6、WARNSettingsFactory:109-Couldnotobtainconnectionmetadata
java.sql.SQLException:Accessdeniedforuser'root'@'localhost'(usingpassword:YES)
这个问题有是一个粗心的问题了:由于hibernate.cfg.xml文件是从其他地方拷过来的,对于其中的属性没有进行修改,因此,这个错误是密码错误。回到hibernate.cfg.xml中,把错误改了,顺便把数据库名改了,错误解决。

7、满以为可以顺利了,不过还是遇到了一个问题:乱码。对于乱码的解决现在已经不是问题,点一下这里。
至此,SSHDemo运行成功!

从这些错误中可以得到以下启示:
启动tomcat的时候,先初始化web.xml文件,再通过applicationContext.xml文件注入对象,在这个注入的过程中,首先检查注入的类是否存在,然后检查与Hibernate和Struts的集成是否有误。最后才是Hibernate.cfg.xml文件的检查,这个时候也就是连接数据库了。哎呀,过程中,struts-config.xml文件配置正确,不知道这个文件在什么时候初始化哩。不过推测是在applicationContext.xml之前。这个推论是有依据的:在applicationContext.xml文件中最先注入的就是Action,而Action又是在struts-config.xml中配置的,你说我的推论有没有道理呢?有兴趣的童鞋可以验证以下这个推论。俺就不研究了。到这里,SSH:struts+Spring+Hibernate与MVC的三层对应为:V、C、M。MVC的调用关系为V->C->M,启动tomcat初始化的配置文件顺序又是struts->spring->hibernate.因此……你懂得。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐