1. 导入相关jar包
1.1 Struts2的jar包
- 基本jar包:struts-2.3.24\apps\struts2-blank\WEB-INF\lib
- 常用扩展jar包:struts-2.3.24\lib
- struts2-convention-plugin-2.3.24.jar 注解开发包
- struts2-json-plugin-2.3.24.jar json转换包
- struts2-spring-plugin-2.3.24.jar spring整合struts2开发包
1.2 Hibernate的jar包
-
基本jar包:hibernate-release-5.0.7.Final\lib\required
注意:struts2与hibernate的javassist包重复了,删除一个低版本的,否则会出错
-
数据库驱动jar包
-
日志记录包:用struts2中的日志记录包即可
-
C3P0连接池包 :hibernate-release-5.0.7.Final\lib\optional\c3p0
1.3 Spring的jar包
-
基本开发(IOC)
- 基本包: spring-framework-4.2.4.RELEASE-dist\spring-framework-4.2.4.RELEASE\libs
- 依赖日志包:spring-framework-3.0.2.RELEASE-dependencies
- 基本包: spring-framework-4.2.4.RELEASE-dist\spring-framework-4.2.4.RELEASE\libs
-
AOP开发
-
数据库开发
-
整合web项目
2. 引入web.xml配置文件
2.1 web.xml
- 在web.xml中配置指定Spring配置文件的位置
<!-- 配置上下文环境参数 -->
<!-- 配置上下文环境参数 -->
<context-param>
<!-- Spring配置文件位置参数名,固定写法 -->
<param-name>contextConfigLocation</param-name>
<!-- [classpath:路径,classpath:路径,...] 路径支持*模糊匹配,多个配置文件用逗号隔开 -->
<!-- 可以读取所有拆分的spring配置文件,主配置文件中不再需要引入拆分文件 -->
<param-value>classpath:com/config/spring/applicationContext*.xml</param-value>
</context-param>
- 在web.xml中配置ContextLoaderListener监听器
<!-- 配置上下文加载监听器,用于初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 在web.xml中配置OpenSessionInViewFilter
<!-- Session管理过滤器,必须配置Strus2的过滤器前面 -->
<!-- 在一次【请求】的完整周期中,所使用的Hibernate Session是唯一的且一直保持开启的可用状态,可以解决延迟加载问题 -->
<!-- 在JSP页面延迟加载关联数据,必须由action转发到JSP才能正确加载数据,因为Session范围只在request中 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<!-- 过滤所有以action结尾的请求 -->
<url-pattern>*.action</url-pattern>
</filter-mapping>
- 在web.xml中配置struts2的核心过滤器
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<!-- 配置struts配置文件路径 -->
<!-- ssh中必须加上struts-default.xml,struts-plugin.xml -->
<!-- struts配置文件路径不用加classpath前缀,不能用*模糊匹配 (加了classpath路径变长很多而且也不能*模糊匹配)-->
<param-value>struts-default.xml,struts-plugin.xml,com/config/struts/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. Spring整合Struts
- 拆分后的spring配置文件applicationContext-grade.xml
<bean id="gradeDao" class="com.dao.impl.GradeDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="gradeService" class="com.service.impl.GradeServiceImpl">
<property name="gradeDao" ref="gradeDao"></property>
</bean>
<!-- Action类的创建交给Spring -->
<!-- Action创建必须是多例的scope="prototype",因为每次请求的数据都是不一样的 -->
<bean id="gradeAction" class="com.action.GradeAction" scope="prototype">
<!-- 注入service对象属性 -->
<property name="gradeService" ref="gradeService"></property>
</bean>
- struts主配置文件struts.xml
<?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>
<!-- 此处忽略全局配置和常量配置 -->
<!-- 拆分引入多个配置文件 -->
<include file="com/config/struts/struts-*.xml"></include>
</struts>
- 拆分后的struts配置文件struts-grade.xml
<?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="grade" extends="struts-default" namespace="/">
<!-- class引入的是spring创建的gradeAction实例 -->
<action name="grade_*" class="gradeAction" method="{1}">
<result name="{1}_success">/grade_{1}_success.jsp</result>
</action>
</package>
</struts>
- Action类
package com.action;
import com.domain.Grade;
import com.opensymphony.xwork2.ActionSupport;
import com.service.GradeService;
public class GradeAction extends ActionSupport {
private GradeService gradeService;
private Grade grade;
public Grade getGrade() {return grade;}
public void setGrade(Grade grade) {this.grade = grade;}
public void setGradeService(GradeService gradeService) {
this.gradeService = gradeService;
}
public String add(){
gradeService.add(grade);
return "add_success";
}
}
4. Spring整合Hibernate
4.1 方式一:保留hibernate的配置文件
- hiberante.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://localhost:3306/ssh01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置C3P0连接池 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位,
如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property>
<!-- 运行程序时是否在控制台显示sql语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="format_sql">true</property>
<!-- 映射文件路径 -->
<mapping resource="com/domain/Student.hbm.xml" />
<mapping resource="com/domain/Result.hbm.xml" />
<mapping resource="com/domain/Subject.hbm.xml" />
<mapping resource="com/domain/Grade.hbm.xml" />
</session-factory>
</hibernate-configuration>
- 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">
<!-- Spring整合Hibernate -->
<!-- 引入Hibernate的配置的信息=============== -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入hibernate的配置文件 -->
<property name="configLocation" value="classpath:com/config/hibernate.cfg.xml"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务增强,并指定事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<!-- 如果出现异常BeanNotOfRequiredTypeException需要把aop:config中的 proxy-target-class属性设置为true -->
<aop:config >
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="servicePointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
</beans>
4.2 抛弃hibernate配置文件,由Spring直接管理hibernate
- 把数据连接信息放到jdbc.properties文件中
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh01
jdbc.username=root
jdbc.password=root
- spring配置文件管理hibernate
<?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">
<!-- 1.引入外部属性文件 -->
<context:property-placeholder location="classpath:com/config/jdbc.properties"/>
<!-- 2.配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Spring整合Hibernate -->
<!-- 3.引入Hibernate的配置的信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 设置映射文件 -->
<property name="mappingLocations">
<value>classpath:/com/domain/*.hbm.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务增强,并指定事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<!-- 如果出现异常BeanNotOfRequiredTypeException需要把aop:config中的 proxy-target-class属性设置为true -->
<aop:config >
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="servicePointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
</beans>
5. 完整例子
5.1 项目目录
5.2 整合后的jar包
5.3 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">
<!-- 配置上下文环境参数 -->
<context-param>
<!-- Spring配置文件位置参数名,固定写法 -->
<param-name>contextConfigLocation</param-name>
<!-- [classpath:路径,classpath:路径,...] 路径支持*模糊匹配,多个配置文件用逗号隔开 -->
<!-- 可以读取所有拆分的spring配置文件,主配置文件中不再需要引入拆分文件 -->
<param-value>classpath:com/config/spring/applicationContext*.xml</param-value>
</context-param>
<!-- 配置上下文加载监听器,用于初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Session管理过滤器,必须配置Strus2的过滤器前面 -->
<!-- 在一次【请求】的完整周期中,所使用的Hibernate Session是唯一的且一直保持开启的可用状态,可以解决延迟加载问题 -->
<!-- 在JSP页面延迟加载关联数据,必须由action转发到JSP才能正确加载数据,因为Session范围只在request中 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<!-- 过滤所有以action结尾的请求 -->
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<!-- 配置struts配置文件路径 -->
<!-- ssh中必须加上struts-default.xml,struts-plugin.xml -->
<!-- struts配置文件路径不用加classpath前缀,不能用*模糊匹配 (加了classpath路径变长很多而且也不能*模糊匹配)-->
<param-value>struts-default.xml,struts-plugin.xml,com/config/struts/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5.4 数据库连接信息jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh01
jdbc.username=root
jdbc.password=root
5.5 spring配置文件
- 主配置文件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">
<!-- 1.引入外部属性文件 -->
<context:property-placeholder location="classpath:com/config/jdbc.properties"/>
<!-- 2.配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Spring整合Hibernate -->
<!-- 3.引入Hibernate的配置的信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 设置映射文件 -->
<property name="mappingLocations">
<value>classpath:/com/domain/*.hbm.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务增强,并指定事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<!-- 如果出现异常BeanNotOfRequiredTypeException需要把aop:config中的 proxy-target-class属性设置为true -->
<aop:config >
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="servicePointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
</beans>
- 功能模块配置文件applicationContext-grade.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">
<!-- 拆分后的配置文件可以根据模块或分工定义多个类的实例 -->
<bean id="gradeDao" class="com.dao.impl.GradeDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="gradeService" class="com.service.impl.GradeServiceImpl">
<property name="gradeDao" ref="gradeDao"></property>
</bean>
<!-- Action类的创建交给Spring -->
<!-- Action创建必须是多例的scope="prototype",因为每次请求的数据都是不一样的 -->
<bean id="gradeAction" class="com.action.GradeAction" scope="prototype">
<!-- 注入service对象属性 -->
<property name="gradeService" ref="gradeService"></property>
</bean>
</beans>
5.6 struts配置文件
- 主配置文件struts.xml
<?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>
<!-- 此处省略其他全局配置和常量等配置 -->
<!-- 拆分引入多个配置文件 -->
<include file="com/config/struts/struts-*.xml"></include>
</struts>
- 功能模块配置文件struts-grade.xml
<?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>
<!-- 拆分后的配置文件可以根据模块或分工定义多Action -->
<package name="grade" extends="struts-default" namespace="/">
<!-- class引入的是spring创建的gradeAction实例 -->
<action name="grade_*" class="gradeAction" method="{1}">
<result name="{1}_success">/grade_{1}_success.jsp</result>
</action>
</package>
</struts>
5.7 Dao部分
- Dao接口
package com.dao;
import com.domain.Grade;
public interface GradeDao {
void add(Grade grade);
}
- Dao实现类
package com.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.dao.GradeDao;
import com.domain.Grade;
//继承HibernateDaoSupport类可以使用HibernateTemplate的方法
//需要注入sessionFactory属性
public class GradeDaoImpl extends HibernateDaoSupport implements GradeDao{
@Override
public void add(Grade grade) {
getHibernateTemplate().save(grade);
}
}
5.8 Service部分
- Service接口
package com.service;
import com.domain.Grade;
public interface GradeService {
void add(Grade grade);
}
- Service实现类
package com.service.impl;
import com.dao.GradeDao;
import com.domain.Grade;
import com.service.GradeService;
public class GradeServiceImpl implements GradeService {
private GradeDao gradeDao;
public void setGradeDao(GradeDao gradeDao) {
this.gradeDao = gradeDao;
}
@Override
public void add(Grade grade) {
gradeDao.add(grade);
}
}
5.9 Action部分
package com.action;
import com.domain.Grade;
import com.opensymphony.xwork2.ActionSupport;
import com.service.GradeService;
public class GradeAction extends ActionSupport {
private GradeService gradeService;
private Grade grade;
public Grade getGrade() {return grade;}
public void setGrade(Grade grade) {this.grade = grade;}
public void setGradeService(GradeService gradeService) {
this.gradeService = gradeService;
}
public String add(){
gradeService.add(grade);
return "add_success";
}
}
5.10 前端页面
- grade_add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action="/SSH04/grade_add.action" method="post">
班级名称:<input name="grade.name"/><br/>
<input type="submit" value="添加"/>
</form>
</body>
</html>
- grade_add_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
${grade.name }
</body>
</html>