这两天从网上 down 了两个 Struts+Spring+Hibernate 的例子,一个是 Spring Live 中第二章的 myusers,一个是 Wiring your web application with open source java(这个文档网上有中文也有英文的) 中的例子,这里我取名为 SSHTest。 在 MyEclipse 中建立这两个工程并调试成功后,我看到这两个工程在组合 Struts、Spring、Hibernate 的时候有点点不同,下面就三个配置文件(web.xml、struts-config.xml、applicationContext.xml、)来看他们的不同:
myusers 的 web.xml 没有引入什么特别的。
SSHTest 的 web.xml : 通过: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener--> 或: <servlet> <servlet-name>SpringContextServlet</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> Web 容器会自动加载 /WEB-INF/applicationContext.xml 初始化 ApplicationContex t实例; 也可以通过 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml</param-value> </context-param> 使 Web 容器加载指定名称路径的 Spring 配置文件。