准备工作:struts2.3.34+spring4.2.2+hibernate3.3.2
导入需要的开发包:
struts开发包---注意:javassist-3.18.1-GA.jar包与hibernate中的重复(只保留高版本即可)
hibernate开发包
此外还需要在hibernate解压包中找到lib/optional/c3p0文件夹,然后将c3p0-0.9.2.1.jar和mchange-commons-java-0.2.3.4.jar拷贝到工程lib目录下
spring开发包
除了javadoc.jar和sources.jar包名结尾的包,其他包全部拷贝到工程项目lib中
最后别忘了将mysql驱动包拷贝到lib中
2、web.xml
Spring提供了ContextLoaderListener,该监听器实现了ServletContextListener接口,他在Web应用程序启动时被触发。当他创建时会自动查找WEB-INF/下的applicationContext.xml,所以当只有一个配置文件且文件名为applicationContext.xml时,则只需要在web.xml文件中配置ContextLoaderListener监听器即可.当有多个配置文件需要载入,则应该使用<context-param>元素指定配置文件的文件名,ContextLoaderListener加载时,会查找名为contextConfigLocation的初始化参数。当Web应用程序启动时先读取web.xml文件,然后创建spring容器,之后根据配置文件内容,装配Bean实例。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>sklm_2</display-name> 4 <welcome-file-list> 5 <!-- 6 <welcome-file>index.html</welcome-file> 7 <welcome-file>index.htm</welcome-file> 8 <welcome-file>index.jsp</welcome-file> 9 <welcome-file>default.html</welcome-file> 10 <welcome-file>default.htm</welcome-file> 11 --> 12 <welcome-file>home.jsp</welcome-file> 13 </welcome-file-list> 14 15 <!-- 让spring随web启动而创建的监听器 --> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 <!-- 配置spring配置文件位置参数 --> 20 <context-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:applicationContext.xml</param-value> 23 </context-param> 24 25 <!-- struts2核心配置 --> 26 <filter> 27 <filter-name>struts2</filter-name> 28 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 29 </filter> 30 <filter-mapping> 31 <filter-name>struts2</filter-name> 32 <url-pattern>/*</url-pattern> 33 </filter-mapping> 34 35 </web-app>