现在的位置: 首页 > 综合 > 正文

Spring2.5、Struts2.1、Hibernate3.2、sitemesh、freemarker整合开发常见问题及解决方案

2018年01月25日 ⁄ 综合 ⁄ 共 2921字 ⁄ 字号 评论关闭

1.在搭建SSH开发平台的时候,由MyEclipse自动导入的包存在一些冲突,主要有

 

(1) asm.jar与asm-2.2.3.jar

 

asm-2.2.3是spring所依赖,而asm.jar是hibernate依赖的,应该删除asm-2.2.3.jar这个包

 

(2). commons-logging-1.0.4.jar与commons-logging-api-1.1.jar

 

这两个包中commons-logging-1.0.4.jar是spring和hibernate以及struts都要用的包,但是struts2.1以上的版本还需要commons-logging-api-1.1.jar,其实这两个包几乎一样,commons-logging-api-1.1.jar版本较新而已。

 

(3)异常

严重: Exception starting filter struts2
java.lang.NullPointerException
    at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:203)
   ...

 

可能的原因是spring的默认配置文件applicationContext.xml改了名,或者没有放在classpath的根路径下面,并且没有在web.xml文件中配置spring的监听器,解决办法在web.xml中添加

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

 

(4)异常

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]


可能的原因是虽然配置了spring的监听器,但是没有在web.xml中配置contextConfigLocation来指定spring配置文件列表及相应的具体路径。解决的办法是在web.xml文件中添加,例如:

<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/csdn-spring*.xml</param-value>
 </context-param>


(5)异常

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection

可能的原因是缺少相应的JDBC驱动包,请把驱动包拷贝到/WEB-INF/lib下面,或者是数据源的配置有问题,例如用户名、者密码或者URL错误。

(6)异常

javax.servlet.ServletException: Error applying decorator: null
	org.apache.struts2.sitemesh.FreeMarkerPageFilter.applyDecorator(FreeMarkerPageFilter.java:164)
	org.apache.struts2.sitemesh.TemplatePageFilter.applyDecorator(TemplatePageFilter.java:116)
	com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:62)
	org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

 

 

Struts2的sitemesh插件struts2-sitemesh-plugin-2.1.6的一个BUG,是FreeMarkerPageFilter的getLocale方法没有对invocation添加非空判断造成的,解决办法是解压包,然后找到org.apache.struts2.sitemesh.FreeMarkerPageFilter这个类,将

 修改为:

抱歉!评论已关闭.