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

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、Unexpectedexception parsing XML document from file [E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-common.xml];nested exception is java.lang.NoClassDefFoundError:org/aspectj/weaver/tools/PointcutPrimitive

这个错误是因为applicationContext-common.xml这个配置文件写的有误:在配置pointcut的时候

<!-- 那些类那些方法使用事务 -->
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* manager.*.*(..))"/>
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
	</aop:config>


 

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

 

继续重启tomcat,继续错误:

4、Error creating beanwith name '/addDepartment' defined in file [E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-actions.xml]:Cannot resolve reference to bean 'department' while setting bean property'department'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'department' is defined

这个是applicationContext.xml文件配置有误,也就是类的注入有问题,是缺少类的注入。根据错误提示,把缺少的类加上就行了。

 

5、下面的错误报告很多,不过问题不大,主要是在最后一句话:sessionFactory or hibernateTemplate is required。由于dao层继承HibernateSupport,将session的管理都放到了Spring中,因此,在配置applicationContext.xml文件的时候需要在注入dao层类的时候把sessionFactory作为一个属性来注入。而我没有注入,因此错误出现。由于dao层有错,manager层中的类注入dao的时候就会报错,而servlet,也就是Action又要注入manager,因此错误堆栈打出来的是先从Action的注入开始到manager,最后是dao。错误报告如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name'/addDepartment' defined in file [E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-actions.xml]: Cannot resolve reference to bean'departmentManager' while setting bean property 'departmentManager'; nestedexception isorg.springframework.beans.factory.BeanCreationException: Error creating bean with name'departmentManager' defined in file [E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-beans.xml]: Cannot resolve reference to bean'departmentDao' while setting bean property 'departmentDao'; nested exceptionisorg.springframework.beans.factory.BeanCreationException: Error creating bean with name'departmentDao' defined in file [E:\myWork\dcy\test\WorkPlace\apache-tomcat-5.5.26\webapps\evaluation\WEB-INF\classes\applicationContext-dao.xml]: Invocation of init method failed; nestedexception is java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required

Caused by:

 

6、WARNSettingsFactory:109 - Could not obtain connection metadata

java.sql.SQLException: Access denied for user 'root'@'localhost'(using password: YES)

这个问题有是一个粗心的问题了:由于hibernate.cfg.xml文件是从其他地方拷过来的,对于其中的属性没有进行修改,因此,这个错误是密码错误。回到hibernate.cfg.xml中,把错误改了,顺便把数据库名改了,错误解决。

 

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

至此,SSH Demo运行成功!

 

从这些错误中可以得到以下启示:

启动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.因此……你懂得。



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值