Struts2框架
struts-2.3.24\apps\struts2-blank\WEB-INF\lib*.jar – Struts2需要的所有jar包struts2-spring-plugin-2.3.24.jar —Struts2整合Spring的插件包Hibernate框架
hibernate-release-5.0.7.Final\lib\required*.jar – Hibernate框架需要的jar包slf4j-api-1.6.1.jar – 日志接口slf4j-log4j12-1.7.2.jar – 日志实现mysql-connector-java-5.1.7-bin.jar – MySQL的驱动包c3p0-0.9.2.1.jar,hibernate-c3p0-5.0.7.Final.jar,mchange-commons-java-0.2.3.4.jar – 整合c3p0需要用到的3个jar包Spring框架
IOC核心包4个日志包2个AOP+AspectJ 共4个包spring-jdbc-4.2.4.RELEASE.jar,spring-tx-4.2.4.RELEASE.jar – JDBC模板和事务核心包spring-test-4.2.4.RELEASE.jar – Spring整合JUnit测试包spring-orm-4.2.4.RELEASE.jar – Spring整合Hibernate核心包spring-web-4.2.4.RELEASE.jar – Spring整合Struts2核心包以上所有jar包都在之前介绍struts、hibernate、spring的文章中找到。
这里我将所有SSH整合需要用到的jar包都上传到百度云,一共49个jar包,下载链接 ,提取码:w9mx ,有需要的可自行下载。
Struts2的jar包中有一个javassist-3.11.0.GA.jar,而Hibernate的jar包中也有一个javassist-3.18.1-GA.jar,因此整合的时候会导致包冲突的问题,所以需要删除其中一个,这里我保留高版本的。
1.Struts2框架 在web.xml中配置核心的过滤器
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>在src目录下创建struts.xml,用来配置Action
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> </package> </struts>2.Hibernate框架 在src目录创建hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 必须配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///ssh_01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1234</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置C3P0的连接池 --> <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!-- 映射配置文件 --> <mapping resource="blog/csdn/net/mchenys/domain/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>在JavaBean所在的包下创建映射的Customer.hbm.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="blog.csdn.net.mchenys.domain.Customer" table="cst_customer"> <id name="id" column="cust_id"> <generator class="native"/> </id> <property name="name" column="cust_name"/> <property name="source" column="cust_source"/> <property name="level" column="cust_level"/> <property name="phone" column="cust_phone"/> </class> </hibernate-mapping>3.Spring框架 在web.xml配置整合WEB的监听器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>在web.xml配置一个过滤器
<!--解决延时加载session关闭的问题, 要注意需要在struts2的核心过滤器之前进行配置 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>在src目录下创建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> </beans>在src目录下log4j.proerties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=info, stdout完成后,项目的包结构如下:
完整的web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <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_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- spring的web监听器,用于程序启动的时候初始化applicationContext.xml --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--解决延时加载session关闭的问题, 要注意需要在struts2的核心过滤器之前进行配置 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2的核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>