Skip to content

21. 结论

本文已探讨了 Struts 2 框架的基础知识,并展示了一个融合多种技术的应用程序。初学者或中级水平的读者可从本文中获得学习与进步的素材。目前仍需一本参考书。我再次推荐以下著作:

Struts 2 in Action》,作者为 Donald Brown、Chad Michael Davis 和 Scott Stanlick,由 Manning 出版社出版。

最后,我想列举一下在编写本文示例时遇到的问题。我之前已经提到了Struts 2的一些意外行为,特别是在输入数字和日期时。以下是遇到的一些其他问题:

将应用程序 [pam-01] 从 Tomcat 服务器中卸载:

Image

此时 Apache 控制台会显示以下日志:

1
2
3
4
5
6
7
8
janv. 05, 2012 11:18:58 AM org.springframework.context.support.AbstractApplicationContext doClose
Infos: Closing org.springframework.web.context.support.XmlWebApplicationContext@57488b9c: display name [Root WebApplicationContext]; startup date [Thu Jan 05 11:18:45 CET 2012]; root of context hierarchy
janv. 05, 2012 11:18:58 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
Infos: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@78ae9d8d: defining beans [config,metier]; root of factory hierarchy
janv. 05, 2012 11:18:58 AM org.apache.tiles.access.TilesAccess setContainer
Infos: Removing TilesContext for context: org.apache.catalina.core.ApplicationContextFacade
janv. 05, 2012 11:19:00 AM org.apache.catalina.startup.HostConfig checkResources
Infos: Repli (undeploy) de l'application web ayant pour chemin de contexte /pam-01

卸载操作已成功完成。

以同样的方式卸载 [pam-02]。此时日志如下:

janv. 05, 2012 11:21:07 AM org.springframework.context.support.AbstractApplicationContext doClose
Infos: Closing org.springframework.web.context.support.XmlWebApplicationContext@5d9131fa: display name [Root WebApplicationContext]; startup date [Thu Jan 05 11:11:10 CET 2012]; root of context hierarchy
janv. 05, 2012 11:21:07 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
Infos: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b66d238: defining beans [config,metier,employeDao,indemniteDao,cotisationDao,entityManagerFactory,dataSource,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,txManager,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0]; 工厂层级结构的根节点
janv. 05, 2012 11:21:07 AM org.springframework.orm.jpa.AbstractEntityManagerFactoryBean destroy
Infos: Closing JPA EntityManagerFactory for persistence unit 'pam-PU'
janv. 05, 2012 11:21:07 AM org.hibernate.impl.SessionFactoryImpl close
Infos: closing
janv. 05, 2012 11:21:07 AM org.apache.tiles.access.TilesAccess setContainer
Infos: Removing TilesContext for context: org.apache.catalina.core.ApplicationContextFacade
janv. 05, 2012 11:21:07 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
Grave: The web application [/pam-02] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
janv. 05, 2012 11:21:07 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
Grave: The web application [/pam-02] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.
janv. 05, 2012 11:21:07 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
Grave: The web application [/pam-02] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.
janv. 05, 2012 11:21:08 AM org.apache.catalina.startup.HostConfig checkResources
Infos: Repli (undeploy) de l'application web ayant pour chemin de contexte /pam-02

第 12、14、16 行报告了严重错误。其中两条提示可能存在内存泄漏。实际上,应用程序 [pam-02] 确实存在内存泄漏。过了一会儿,该应用程序便无法继续使用。此时必须关闭 NetBeans 并重新部署应用程序。

这些错误均与线程相关。[pam-02]应用程序本身并未使用线程。因此,应怀疑是所使用的框架(Struts 2、Spring 2.5、Tiles)所致。可以合理地排除Tiles,因为它很可能不支持线程管理。剩下的就是Struts和Spring了。

于是我们进行另一项测试:

我们将 [web] 层改用 JSF 框架实现,而非 Struts 2。其他配置均保持不变。结果却出现了与 Struts 2 相同的错误信息。

因此,问题似乎出在Spring上,或者出在Spring在此处使用的dbcp连接池上,又或者是我自己没有正确使用Spring。我可能遗漏了某项Spring配置,该配置本可以清理已创建的线程,也可能……因此,这个内存泄漏问题的解决方案仍有待寻找。

另一项尝试。IDE NetBeans 随 Tomcat 和 GlassFish 服务器一同安装。让我们尝试在 GlassFish 上运行本文档中创建的应用程序:

  • 在 [2] 中,项目属性为 [exemple-08] [1]
  • 在 [3] 中,选择 Web 服务器

以下是在浏览器中获取的页面:

预期的可移植性并未实现。这里有两个错误:

  • 在 [1] 中,国际化处理未成功
  • 在 [2] 中,出现了一个异常

本文档中开发的大多数应用程序都出现了这两个相同的错误。我尚未尝试解决这一新问题。