一:引入jar包
1.引入Struts基本的jar包
2.引入Hibernate的基本的jar包
3.引入Spring基本的jar包
总的jar包如下:
antlr-2.7.7.jar
aopalliance-1.0.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
aspectjrt-1.7.4.jar
aspectjweaver-1.7.4.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
commons-fileupload-1.3.2.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
freemarker-2.3.22.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.11.0.GA.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mysql-connector-java-5.1.24-bin.jar
ognl-3.0.19.jar
spring-aop-4.0.4.RELEASE.jar
spring-aspects-4.0.4.RELEASE.jar
spring-beans-4.0.4.RELEASE.jar
spring-build-src-4.0.4.RELEASE.jar
spring-context-4.0.4.RELEASE.jar
spring-context-support-4.0.4.RELEASE.jar
spring-core-4.0.4.RELEASE.jar
spring-expression-4.0.4.RELEASE.jar
spring-framework-bom-4.0.4.RELEASE.jar
spring-instrument-4.0.4.RELEASE.jar
spring-instrument-tomcat-4.0.4.RELEASE.jar
spring-jdbc-4.0.4.RELEASE.jar
spring-jms-4.0.4.RELEASE.jar
spring-messaging-4.0.4.RELEASE.jar
spring-orm-4.0.4.RELEASE.jar
spring-oxm-4.0.4.RELEASE.jar
spring-test-4.0.4.RELEASE.jar
spring-tx-4.0.4.RELEASE.jar
spring-web-4.0.4.RELEASE.jar
spring-webmvc-4.0.4.RELEASE.jar
spring-webmvc-portlet-4.0.4.RELEASE.jar
spring-websocket-4.0.4.RELEASE.jar
struts2-core-2.3.33.jar
struts2-spring-plugin-2.3.33.jar
xwork-core-2.3.33.jar
二:引入相应的配置文件
1.Struts框架的配置文件
web.xml
首先配置Struts2的过滤器 。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>TYRenting-Master</display-name>
<!-- 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>
struts.xml
先创建好空的struts配置文件
<?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="ssh" extends="struts-default" namespace="/">
</package>
</struts>
2.Hibernate框架的配置文件
hibernate.xml(在ssh整合中该配置文件可以省略)
映射文件(后面框架整合的时候再创建)
3.Spring框架的配置文件
web.xml 配置核心的监听器
在前面的web.xml的基础上加上spring框架的核心监听器,指定加载classpath:applicationContext.xml下面的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>TYRenting-Master</display-name>
<!-- Spring的框架的核心监听器 -->
<!-- 默认加载WEB-INF里面的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定加载classpath下面spring的核心配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml
先创建空白的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>
三:创建包文件结构
如图所示:分别建立action, service, service.impl ,dao , dao.impl ,domain 包
LoginAction.java的代码
package action;
import service.impl.LoginServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.User;
/*
* 用户登录的Action类
*/
public class LoginAction extends ActionSupport implements ModelDriven<User>{
//模型驱动使用的类
private User user = new User() ;
@Override
public User getModel() {
return user;
}
//Struts和Spring整合过程中按名称自动注入的业务层类
//Action层调用业务层的类需要引入struts2-spring-plugin-2.3.33.jar
private LoginServiceImpl loginServiceImpl;
public void setLoginServiceImpl(LoginServiceImpl loginServiceImpl) {
this.loginServiceImpl = loginServiceImpl;
}
/*
* 框架整合测试方法
*/
public String save()
{
System.out.println("Action中的save方法执行了。。。");
User testUser = new User(0,"13798957741","123",0);
loginServiceImpl.save(testUser);
return NONE ;
}
}
LoginServiceImpl.java的代码
package service.impl;
import org.springframework.transaction.annotation.Transactional;
import dao.impl.LoginDaoImpl;
import domain.User;
/*
* 用户登录的业务层的实现类
*/
@Transactional
public class LoginServiceImpl {
//业务层注入DAO的类
private LoginDaoImpl loginDaoImpl ;
public void setLoginDaoImpl(LoginDaoImpl loginDaoImpl) {
this.loginDaoImpl = loginDaoImpl;
}
public void save(User user) {
System.out.println("service中的save方法执行了。。。");
loginDaoImpl.save(user) ;
}
}
LoginDaoImpl.java的代码
package dao.impl;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import domain.User;
/*
* 用户登录的DAO的实现类
*/
public class LoginDaoImpl extends HibernateDaoSupport{
public void save(User user) {
System.out.println("DAO中的save方法执行了。。。");
this.getHibernateTemplate().save(user);
}
}
四:Struts2整合Spring框架(两两整合)
有两种方式:
1.Action的类由Struts2自身去创建,在struts.xml完成配置。
在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" >
<!-- 配置业务层的类 -->
<bean id="loginServiceImpl" class="service.impl.LoginServiceImpl">
<property name="loginDaoImpl" ref="loginDaoImpl"></property>
</bean>
<!-- 配置DAO的类 -->
<bean id="loginDaoImpl" class="dao.impl.LoginDaoImpl">
</bean>
</beans>
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>
<package name="ssh" extends="struts-default" namespace="/">
<!-- 1.Action的类由Struts2自身去创建,在struts.xml完成配置 -->
<action name="loginAction_*" class="action.LoginAction" method="{1}">
</action>
</action>
</package>
</struts>
2.Action的类交给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" >
<!-- Action的类交给Spring框架创建 -->
<!-- 建议使用该方式 -->
<bean id="loginAction" class="action.LoginAction" scope="prototype">
<!-- 手动注入Service -->
<property name="loginServiceImpl" ref="loginServiceImpl"></property>
</bean>
<!-- 配置业务层的类 -->
<bean id="loginServiceImpl" class="service.impl.LoginServiceImpl">
<property name="loginDaoImpl" ref="loginDaoImpl"></property>
</bean>
<!-- 配置DAO的类 -->
<bean id="loginDaoImpl" class="dao.impl.LoginDaoImpl">
</bean>
</beans>
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>
<package name="ssh" extends="struts-default" namespace="/">
<!-- 2.Action的类交给Spring框架创建 -->
<!-- 这里class不需要全路径,只需要spring的对应的类的id即可 -->
<action name="loginAction_*" class="loginAction" method="{1}">
</action>
</package>
</struts>
五:Spring整合Hibernate框架
1.创建实体
package domain;
/*
* 用户登录信息实体
*/
public class User {
private Integer uid;
private String account;
private String password;
private Integer role;
public User() {
super();
}
public User(Integer uid, String account, String password, Integer role) {
super();
this.uid = uid;
this.account = account;
this.password = password;
this.role = role;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
}
2.创建关系映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 26, 2017 9:55:39 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="domain.User" table="USER">
<id name="uid" type="java.lang.Integer">
<column name="UID" />
<generator class="native" />
</id>
<property name="account" type="java.lang.String">
<column name="ACCOUNT" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="role" type="java.lang.Integer">
<column name="ROLE" />
</property>
</class>
</hibernate-mapping>
3.在applicationContext.xml中配置hibernate的核心配置文件。
包括属性的配置,事务的开启,在dao层注入sessionFactory类,这样就可以在dao层使用getHibernateTemplate()来执行数据库的操作了。
<?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" >
<!-- 无Hibernate核心配置文件,直接在spring中配置的方法 -->
<!-- 无需写任何其他的代码,只要运行服务器,自动加载配置文件,便自动创建数据库 -->
<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置Hibernate的相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置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>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载Hibernate中的映射文件 -->
<property name="mappingResources">
<list>
<value>domain/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- Struts和Spring整合的两种方式 -->
<!-- 1.Action的类由Struts2自身去创建,在struts.xml完成配置 -->
<!-- 2.Action的类交给Spring框架创建 -->
<!-- Action的类交给Spring框架创建 -->
<!-- 建议使用该方式 -->
<bean id="loginAction" class="action.LoginAction" scope="prototype">
<!-- 手动注入Service -->
<property name="loginServiceImpl" ref="loginServiceImpl"></property>
</bean>
<!-- 配置业务层的类 -->
<bean id="loginServiceImpl" class="service.impl.LoginServiceImpl">
<property name="loginDaoImpl" ref="loginDaoImpl"></property>
</bean>
<!-- 配置DAO的类 -->
<bean id="loginDaoImpl" class="dao.impl.LoginDaoImpl">
<!-- 注意这里要注入sessionFactory才可以使用Hibernate模板哦! -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
这就是完整的ssh框架的整合的全部内容,如果错误,欢迎指出。