SSH框架之Spring+Struts2+Hibernate整合篇
回顾 -Hibernate框架
ORM: 对象关系映射.把数据库表和JavaBean经过映射的配置文件映射起来,
操做JavaBean对象,经过映射的配置文件生成SQL语句,自动执行.操做数据库.
1: 类名.hbm.xml 映射配置文件. 2: hibernate.cfg.xml 核心配置文件. 3: 使用Hibernate提供的API操做.
Struts2框架 : 和客户端进行交互 1. 在web.xml配置过滤器. 2. struts.xml配置文件.
Spring框架 1. applicationContext.xml配置 2. 核心IOC和AOP 3. 事务管理.
CustomerAction类 在struts.xml配置中配置的 1. Action对象由Struts2框架建立的.
CustomerAction:建立CustomerAction对象,由Struts2框架建立的 ---> Spring的IOC容器中对象,找customerService对象,默认按名称找的.
2. Action对象也能够由Spring框架类建立 <bean id="customerAction" class="com.baidu.customer.action.CustomerAction" scope="prototype"><action name="customerAction_*" class="customerAction" method="{1}">/jsp/customer/add.jsp
day67_Spring_05
第1章整合前的准备 1.1整合说明
a.独立式整合指的是三个框架都使用本身的配置文件。
b.引入式整合指的是hibernate主配置文件中的内容都配置到spring配置文件中
c.在整合过程当中,确保每步都运行成功,而后在继续往下作。
d.整合中使用的案例是客户的保存和列表查询操做。
e.后面的三种整合方式都基于1.2中的环境准备。 1.2环境准备 1.2.1第一步:建立java web工程
此处使用Servlet2.5规范。 1.2.2第二步:建立数据库和表结构
create database crm;
use crm; /*建立客户表*/
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_address` varchar(128) DEFAULT NULL COMMENT '客户联系地址',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '客户联系电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 1.2.3第三步:编写实体类 /**
* 客户的实体类(数据模型) */
public class Customer implements Serializable { private Long custId; private String custName; private String custSource; private String custIndustry; private String custLevel; private String custAddress; private String custPhone; public Long getCustId() { return custId;
} public void setCustId(Long custId) { this.custId = custId;
} public String getCustName() { return custName;
} public void setCustName(String custName) { this.custName = custName;
} public String getCustSource() { return custSource;
} public void setCustSource(String custSource) { this.custSource = custSource;
} public String getCustIndustry() { return custIndustry;
} public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry;
} public String getCustLevel() { return custLevel;
} public void setCustLevel(String custLevel) { this.custLevel = custLevel;
} public String getCustAddress() { return custAddress;
} public void setCustAddress(String custAddress) { this.custAddress = custAddress;
} public String getCustPhone() { return custPhone;
} public void setCustPhone(String custPhone) { this.custPhone = custPhone;
}
@Override public String toString() { return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress + ", custPhone=" + custPhone + "]";
}
} 1.2.4第四步:编写业务层接口和实现类 /**
* 客户的业务层接口 */
public interface ICustomerService { /**
* 查询全部客户
* @return
*/
List findAllCustomer();
/**
* @param customer */
void saveCustomer(Customer customer);
} /**
* 客户的业务层实现类 */
public class CustomerServiceImpl implements ICustomerService { private ICustomerDao customerDao; public void setCustomerDao(ICustomerDao customerDao) { this.customerDao = customerDao;
}
@Override public List findAllCustomer() { return customerDao.findAllCustomer();
}
@Override public void saveCustomer(Customer customer) {
customerDao.saveCustomer(customer);
}
} 1.2.5第六步:建立持久层接口 /**
* 客户的持久层接口 */
public interface ICustomerDao {
/**
* 查询全部客户
* @return
*/
List findAllCustomer();
/**
* 保存客户
* @param customer */
void saveCustomer(Customer customer);
}
注意:作上述操做时,并不须要导入任何jar包。
第2章基于XML的独立式整合 2.1保证spring框架在web工程中独立运行 2.1.1第一步:拷贝spring的ioc,aop和事务控制三组jar包 2.1.2第二步:编写spring配置文件并导入约束 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
2.1.3第三步:把业务层和持久层配置到文件中
<bean id="customerDao" class="com.baidu.dao.impl.CustomerDaoImpl"><bean id="customerService" class="com.baidu.service.impl.CustomerServiceImpl">
持久层实现类代码:
此时不要作任何操做,就输出一句话。目的是测试spring框架搭建的结果。 /**
* 客户的持久层实现类 */
public class CustomerDaoImpl implements ICustomerDao {
@Override public List findAllCustomer() {
System.out.println("查询了全部用户"); return null;
}
@Override public void saveCustomer(Customer customer) {
System.out.println("保存了用户");
}
} 2.1.4第四步:测试spring可否独立运行 /**
* 测试类,测试spring框架能够独立运行 */
public class Spring01Test { public static void main(String[] args) { //1.获取spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.跟Id获取bean对象
ICustomerService customerService = (ICustomerService) ac.getBean("customerService");
customerService.findAllCustomer();
}
} 2.2保证hibernate框架可以在web工程中独立运行 2.2.1第一步:拷贝hibernate必备jar包到工程的lib目录
2.2.2第二步:编写实体类的映射文件
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.baidu.domain">
<class name="Customer" table="cst_customer"><generator class="native">class>2.2.3第三步:编写hibernate主配置文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">com.mysql.jdbc.Driverjdbc:mysql:///crmroperty>
root1234
org.hibernate.dialect.MySQLDialecttruefalseupdateorg.hibernate.connection.C3P0ConnectionProviderthread2.2.4第四步:编写测试类-测试保存客户 /**
* hibernate的测试类
* 保证hibernate框架能够独立运行 */
public class Hibernate02Test {
@Test public void testFindAll(){ //1.读取配置文件
Configuration cfg = new Configuration();
cfg.configure(); //2.根据配置文件获取SessionFactory
SessionFactory factory = cfg.buildSessionFactory(); //3.根据SessionFactory获取一个Session
Session s = factory.getCurrentSession(); //4.开启事务
Transaction tx = s.beginTransaction(); //5.执行操做
Query query = s.createQuery("from Customer");
List list = query.list(); for(Object o : list){
System.out.println(o);
} //6.提交事务 tx.commit(); //7.释放资源 factory.close();
}
@Test public void testSave(){
Customer c = new Customer();
c.setCustName("传智专修学院"); //1.读取配置文件
Configuration cfg = new Configuration();
cfg.configure(); //2.根据配置文件获取SessionFactory
SessionFactory factory = cfg.buildSessionFactory(); //3.根据SessionFactory获取一个Session
Session s = factory.getCurrentSession(); //4.开启事务
Transaction tx = s.beginTransaction(); //5.执行操做 s.save(c); //6.提交事务 tx.commit(); //7.释放资源 factory.close();
}
} 2.3整合spring和hibernate框架 2.3.1明确
a.Spring和Hibernate的整合就是spring接管SessionFactory的建立
b.Spring针对Hiberante的操做有一个封装的对象HibernateTemplate
c.和JdbcTemplate同样,HibernateTemplate也有一个HibernateDaoSupport
d.HibernateTemplate和HibernateDaoSupport都在spring-orm-4.2.4.RELEASE.jar中
e.咱们Dao采用继承HiberanteDaoSupport的方式编写,它同样不能用于注解配置。 2.3.2整合步骤 2.3.2.1第一步:在spring配置文件中配置SessionFactory <bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
2.3.2.2第二步:改造Dao继承HibernateDaoSupport /**
* 客户的持久层实现类 */
public class CustomerDaoImpl extends HibernateDaoSupport implements ICustomerDao {
@Override public List findAllCustomer() {
return (List) getHibernateTemplate().find("from Customer");
}
@Override public void saveCustomer(Customer customer) {
getHibernateTemplate().save(customer);
}
} 2.3.2.3第三步:在spring配置文件中给Dao注入SessionFactory <bean id="customerDao" class="com.baidu.dao.impl.CustomerDaoImpl">2.3.2.4第四步:测试 /**
* 整合spring和hibernate的测试类
* spring整合Junit
* 第一步:拷贝jar包
* spring-junit-4.2.4.jar
* 第二步:使用注解替换运行器(原来junit的main方法)
* @RunWith(支持spring的main方法)
* @ContextConfiguration(指定spring的配置文件位置) */
@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations={"classpath:bean.xml"}) public class SpringHibernate03Test {
@Autowired private ICustomerService customerService;
@Test public void testFindAll(){
List list = customerService.findAllCustomer(); for(Object o : list){
System.out.println(o);
}
}
@Test public void testSave(){
Customer c = new Customer();
c.setCustName("传智学院test");
customerService.saveCustomer(c);
}
}
测试结果:
不管保存仍是查询都运行失败!
按常理来讲,咱们没有配置事务,保存失败是能够理解的。为何查询也会失败呢?
分析缘由:
是因为spring的HibernateTemplate对象在使用Session时,spring建立了Session的代理对象,在这个过程当中,spring对hibernate绑定Session到当前线程的配置不认识了,因此运行失败。 2.3.2.5第五步:修改把Session绑定到当前线程上 是hibernate把session绑定到当前线程上的配置
thread-->
org.springframework.orm.hibernate5.SpringSessionContext
此时再运行刚才的测试:
查询可使用了。保存不能使用,缘由是没有事务。 2.3.3配置Spring的事务 2.3.3.1第一步:配置事务管理器并注入SessionFactory <bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
2.3.3.2第二步:配置事务的通知及通知的属性 2.3.3.3第三步:配置AOP创建切入点表达式和事务通知的关系
再次测试:
此时保存和查询均可以正常使用了。 2.4保证struts2框架可以在web工程中独立运行 2.4.1第一步:拷贝struts2的必备jar包
要把画红线的jar包删掉,由于hibernate中有个高版本的。 2.4.2第二步:在类的类的根路径下编写struts.xml文件并导入约束
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">2.4.3第三步:在web.xml中配置struts2的核心过滤器 struts2<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class>struts2/*2.4.4第四步:导入jsp页面
2.4.5第五步:修改menu.jsp
- 新增客户
2.4.6第六步:在struts.xml中配置action
/jsp/customer/add.jsp2.4.7第七步:编写动做类和方法
/**
* 客户的动做类 */
public class CustomerAction extends ActionSupport implements ModelDriven { private Customer customer = new Customer();
@Override public Customer getModel() { return customer;
}
/**
* 获取添加客户页面
* @return
*/
public String addUICustomer(){ return "addUICustomer";
}
} 2.4.8第八步:测试
运行结果:经过点击【新增客户】能够跳转到客户添加页面 2.5整合spring和struts2 2.5.1明确
a.spring整合struts2就是让spring接管action的建立
b.action是多例的,配置到spring中须要设置scope属性为多例 2.5.2整合步骤 2.5.2.1第一步:拷贝struts2-spring-plugin-2.3.24.jar到lib目录 2.5.2.2第二步:在action中使用构造函数获取Service对象 public CustomerAction(){
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(
ServletActionContext.getServletContext()); //因为动做类是多例的,每次都会建立容器,致使资源的浪费。一个应用应该只有一个容器 System.out.println(ac);
customerService = (ICustomerService) ac.getBean("customerService");
} 2.5.2.3第三步:测试
运行结果:查询客户列表测试经过。保存测试经过。 2.6优化配置 2.6.1配置spring的监听器
在上面2.5.2.2小节中有这么一句:
因为动做类是多例的,每次都会建立容器,致使资源的浪费。一个应用应该只有一个容器
问题:
如何解决呢?
答案:
只要让容器在应用加载时建立,应用卸载时销毁就能够。
问题:
咱们怎么知道应用什么时候加载了呢?
答案:
ServletContext对象建立了,就表示当前应用已经被服务器加载了。
问题:
咱们怎么知道ServletContext对象建立了呢?
答案:
ServletContextListener监听器能够监听到ServletContext对象的建立和销毁。
Spring框架为咱们提供了一个监听器:ContextLoaderListener。
它是ServletContextListener接口的实现类,负责监听ServletContext对象的建立,为咱们建立容器,监听ServletContext对象的销毁,销毁容器。
咱们只须要配置上便可。
ContextLoaderListener在spring-web-4.2.4.RELEASE.jar中
因此咱们要先导入这个jar包。
,在web.xml中配置监听器: <listener-class>
org.springframework.web.context.ContextLoaderListener class>
当配置了此监听器后,就不须要使用Action的构造函数了,能够把构造函数那段删除了。
此监听器只能读取WEB-INF目录中的名称为applicationContext.xml的配置文件。这显然限制了咱们的配置。
咱们能够经过配置全局初始化参数的方式,指定spring配置文件的位置. 2.6.2配置指定spring配置文件的位置
咱们把spring配置文件放到此处,须要配置全局初始化参数: contextConfigLocationclasspath:config/spring/applicationContext.xml2.6.3分文件编写spring配置
咱们写到这里,其实搭建环境已经基本结束了,可是发现spring的配置文件杂乱无章,使咱们在找配置的时候,很难一下找到。因此咱们采用分配置文件编写的方式。 2.6.3.1编写主配置文件引入其余配置文件 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="applicationContext-customer.xml"/>
<import resource="applicationContext-jdbc.xml"/>
<import resource="applicationContext-tx.xml"/>2.6.3.2编写针对需求的配置文件applicationContext-customer.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="customerDao" class="com.baidu.dao.impl.CustomerDaoImpl"><bean id="customerService"
class="com.baidu.service.impl.CustomerServiceImpl">
2.6.3.3编写数据库链接的配置文件applicationContext-jdbc.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation"value="classpath:config/hibernate/hibernate.cfg.xml">/>2.6.3.4编写事务控制的配置文件applicationContext-tx.xml <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<aop:pointcut expression="execution(* com.baidu.service.impl.*.*(..))"id="pt1"/>
2.6.4配置指定struts2配置文件位置
咱们的spring和hibernate配置文件都存到了src/config/的对应包中了,只有struts2配置文件还在类的根路径下,它也能够经过配置的方式指定struts.xml的位置。配置的是过滤器的初始化参数。初始化参数的name和value都是固定写法。 struts2<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class>config
struts-default.xml,struts-plugin.xml,config/struts/struts.xml struts2/*2.6.5分文件编写struts2配置文件
当咱们后面作的模块愈来愈多,struts2一个配置文件写起来也会杂乱无章,因此咱们也能够把struts2的配置文件分开编写。
2.6.5.1编写struts2的主配置文件struts.xml
2.6.5.2针对不一样模块编写不一样的配置文件struts-customer.xml
/jsp/customer/add.jsp/jsp/customer/list.jsp2.6.6管理Action的两种方式
2.6.6.1第一种方式:让struts2本身来管理
此种方式就是在action标签的class属性中提供动做类的全限定类名。/jsp/customer/add.jsp2.6.6.2第二种方式:让spring来管理(实际开发中采用的方式)
此种方式就是在spring配置文件中配置Action,在struts2配置文件action标签的class属性里写bean的id。
spring配置文件:
struts2配置文件:
/jsp/customer/add.jsp第3章基于XML的引入式整合
3.1明确
引入式整合就是把hibernate.cfg.xml中的配置都挪到spring的配置文件中
3.2配置方式
org.hibernate.dialect.MySQLDialecttruefalseupdateorg.springframework.orm.hibernate5.SpringSessionContext。
mappingLocations:配置映射文件的位置,须要写映射文件名称。可使用通配符。
mappingDirectoryLocations:配置映射文件的位置,直接写到包的目录便可。
-->classpath:com/baidu/domain/*.hbm.xml第4章基于注解的整合
4.1明确
a.注解整合仍然使用上面的环境,就是把xml的配置所有换成注解
b.spring的注解整合有两种方式,一种是用xml文件,一种是纯注解。
c.hibernate注解整合是把实体类映射改成JPA注解映射
4.2整合步骤-spring使用xml文件
4.2.1spring配置使用注解实现
4.2.1.1第一步:导入spring的必备jar包
以前的环境已经导入。略。
4.2.1.2第二步:在spring配置文件中导入context名称空间及约束
4.2.1.3第三步:在spring配置文件中配置要扫描的包
4.2.1.4第四步:把action,service和dao都用注解配置
/**
* 客户的动做类 */
@Controller("customerAction")
@Scope("prototype") public class CustomerAction extends ActionSupport implements ModelDriven {
@Autowired private ICustomerService customerService; //action中的方法不变 } /**
* 客户的业务层实现类 */
@Service("customerService") public class CustomerServiceImpl implements ICustomerService {
@Autowired private ICustomerDao customerDao; //service中的方法不变 } /**
* 客户的持久层实现类 */
@Repository("customerDao") public class CustomerDaoImpl implements ICustomerDao { //dao中必须本身定义HibernateTemplate,不能继承HibernateDaoSupport了 @Autowired private HibernateTemplate hibernateTemplate; //dao中的方法不变 } 4.2.1.5第五步:在spring配置文件中配置HiernateTemplate <bean id="hibernateTemplate"
class="org.springframework.orm.hibernate5.HibernateTemplate">
4.2.1.6第六步:在spring配置文件中配置事务管理器 <bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
4.2.1.7第七步:在spring配置文件中开启spring对注解事务的支持 4.2.1.8第八步:在客户的业务层实现类上使用@Transactional注解 /**
* 客户的业务层实现类 */
@Service("customerService")
@Transactional(readOnly=false,propagation=Propagation.REQUIRED) public class CustomerServiceImpl implements ICustomerService {
@Autowired private ICustomerDao customerDao;
@Override
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS) public List findAllCustomer() { return customerDao.findAllCustomer();
}
@Override public void saveCustomer(Customer customer) {
customerDao.saveCustomer(customer);
}
} 4.2.2hibernate映射使用注解配置实现 4.2.2.1实体类映射注解配置 /**
* 客户的实体类
* JPA规范:java 持久化规范
* 注解全都是JPA规范的。
* 导包都须要导入javax.persistence包下的
* */
@Entity
@Table(name="cst_customer") public class Customer implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="cust_id") private Long custId;
@Column(name="cust_name") private String custName;
@Column(name="cust_source") private String custSource;
@Column(name="cust_industry") private String custIndustry;
@Column(name="cust_level") private String custLevel;
@Column(name="cust_address") private String custAddress;
@Column(name="cust_phone") private String custPhone; public Long getCustId() { return custId;
} public void setCustId(Long custId) { this.custId = custId;
} public String getCustName() { return custName;
} public void setCustName(String custName) { this.custName = custName;
} public String getCustSource() { return custSource;
} public void setCustSource(String custSource) { this.custSource = custSource;
} public String getCustIndustry() { return custIndustry;
} public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry;
} public String getCustLevel() { return custLevel;
} public void setCustLevel(String custLevel) { this.custLevel = custLevel;
} public String getCustAddress() { return custAddress;
} public void setCustAddress(String custAddress) { this.custAddress = custAddress;
} public String getCustPhone() { return custPhone;
} public void setCustPhone(String custPhone) { this.custPhone = custPhone;
}
@Override public String toString() { return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress + ", custPhone=" + custPhone + "]";
}
} 4.2.2.2spring中SessionFactory配置修改 <bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
org.hibernate.dialect.MySQLDialect truefalseupdate
org.springframework.orm.hibernate5.SpringSessionContext com.baidu.domain4.2.3struts2配置使用注解实现 4.2.3.1导入struts2注解的jar包 4.2.3.2使用注解配置Action /**
* 客户的动做类 */
@Controller("customerAction")
@Scope("prototype") //-------如下都是struts2的注解-----------
@ParentPackage("struts-default")//指定当前包的父包
@Namespace("/customer")//指定名称空间,访问当前action的全部方法都须要有名称空间
public class CustomerAction extends ActionSupport implements ModelDriven { private Customer customer = new Customer();
@Autowired private ICustomerService customerService;
@Override public Customer getModel() { return customer;
}
/**
* 查询全部客户
* @return
*/
private List customers; //用于配置动做名称
@Action(value="findAllCustomer",results={
@Result(name="findAllCustomer",
type="dispatcher",
location="/jsp/customer/list.jsp")
}) public String findAllCustomer(){
customers = customerService.findAllCustomer(); return "findAllCustomer";
}
/**
* 获取添加客户页面
* @return
*/
@Action(value="addUICustomer",results={
@Result(name="addUICustomer",
location="/jsp/customer/add.jsp")
}) public String addUICustomer(){ return "addUICustomer";
}
/**
* 添加客户
* @return
*/
@Action(value="addCustomer",results={
@Result(name="addCustomer",
type="redirect",
location="/jsp/success.jsp")
}) public String addCustomer(){
customerService.saveCustomer(customer); return "addCustomer";
}
public List getCustomers() { return customers;
} public void setCustomers(List customers) { this.customers = customers;
}
Spring整合Struts2的底层实现原理,由Spring来建立action对象 (原理在包struts2-spring-plugin-2.3.24.jar中) package com.baidu.customer.action; import com.baidu.customer.domain.Customer; import com.baidu.customer.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /**
* 客户模块
* @author Administrator */
public class CustomerAction extends ActionSupport implements ModelDriven{
private Customer customer = new Customer(); public Customer getModel() { return customer;
}
/**
* 跳转到新增页面
* @return
* @throws Exception */
public String initSave() throws Exception { return "initSave";
}
这种方式是ation对象由struts2建立的 // 对象工厂,经过name自动装配对象 customerService(底层经过jar包用常量覆盖struts2的默认配置,开启spring的对象工厂,当浏览器访问的时候 ,访问到action之后再建立CustomerService对象时,用名称name在SpringIOC容器里面找,找到之后返回到Action的set方法的CustomerService里面,给CustomerService赋一个实现类对象值) private CustomerService customerService; public void setCustomerService(CustomerService customerService) { this.customerService = customerService;
} public String save() throws Exception {
System.out.println("WEB层:保存客户...");
customerService.save(customer); return NONE;
}
}