八、框架整合(三) - SSH 整合

一、SSH整合的关键点

	
	Spring,负责对象对象创建
	Struts, 用Action处理请求
	Hibernate:连接数据库,ORM框架

	Spring与Struts框架整合,
			关键点:action对象交给spring创建。
		  
		  			配置spring的监听器(ContextLoaderListener)
		  						加载配置文件,初始化IOC容器。
		  		    
		  		    配置struts的核心过滤器,拦截请求资源。
	
	Spring与hibernate整合
			 关键点:
					1.sessionFactory对象的创建交给spring。
					2,事务管理交给spring。
			
					
	

1.1 配置web.xml (配置struts核心过滤器、配置spring监听器)

		配置web.xml:
						1.配置struts核心过滤器
						2.配置spring监听器
						

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	
	<filter>
		<filter-name>struts2filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
	filter>
	<filter-mapping>
		<filter-name>struts2filter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	
	
	<context-param>
			<param-name>contextConfigLocationparam-name>
				
			
		 <param-value>
						/WEB-INF/classes/bean-*.xml
		 param-value>
			
			
	context-param>
	
	<listener>
			<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
	
web-app>
1.1.1 配置struts核心过滤器
	struts核心过滤器的作用处理拦截请求资源,
	将原本的servlet流程转入struts流程。
	
	<filter>
		<filter-name>struts2filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
	filter>
	<filter-mapping>
		<filter-name>struts2filter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
1.1.2 配置spring监听器
		spring监听器的作用是加载多个配置文件,初始化IOC容器
	
	+++ 配置加载多个配置文件:
				
				<context-param>
						<param-name>contextConfigLocationparam-name>		
							<param-value>
										/WEB-INF/classes/bean-dao.xml,
										/WEB-INF/classes/bean-service.xml,
										/WEB-INF/classes/bean-action.xml
							param-value>
				context-param>
				<listener>
						<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
				listener>
	
	 
	 +++ 配置加载N个配置文件:
				
				<context-param>
						<param-name>contextConfigLocationparam-name>
							<param-value>/WEB-INF/classes/bean*.xmlparam-value> 
				context-param>
				
				<listener>
						<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
				listener>	 

1.2 配置struts.xml (配置struts跳转资源路径,以及将action对象创建交给spring)

	配置struts.xml:
				  1.配置请求路径与资源映射
				  2.将struts的action对象交给spring创建
	
>>>>>> struts.xml


<struts>
    <package name="xxxx" extends="struts-default">
    	
    	
    	<action name="list" class="deptAction" method="sayHello">
    		<result name="list">/index.jspresult>
    	action>
    
    
    
    package> 
struts>
>>>>>> bean-controller.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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="deptAction" class="org.jsoft.action.DeptAction">
    	<property name="deptService"  ref="deptService">property>
    bean>
    
beans>

1.3 配置bean-base.xml (整合hibernate)

		配置bean-base.xml:
						1.将sessionFactory对象交给spring
						2.将事务管理交给spring
		
		spring与hibernate整合有三种方式:
						1.简单整合
								spring创建sessionFactory对象时,
										只需加载hibernate.cfg.xml配置文件即可
								
						2.部分整合
								spring创建sessionFactory对象时,
										数据源配置在spring配置文件中,
										其他配置在hibernate.cfg.xml配置文件中
										
						3.完全整合		
								spring创建sessionFactory对象时,
										所有配置都在spring配置问价那种

二、整合步骤

	
	1) 引入SSH Jar文件
			Struts 核心jar
			Hibernate 核心jar
			Spring
				Core  核心功能
				Web  对web模块支持
				Aop   aop支持
				Orm   对hibernate支持
				Jdbc/tx  jdbc支持包、事务相关包

	2)配置
			Web.xml
					初始化struts功能、spring容器
			Struts.xml   配置请求路径与映射action的关系
			Spring.xml  IOC容器配置
				bean-base.xml     【公用信息】
				bean-service.xml
				bean-dao.xml
				bean-action.xml


	3)开发
		Entity/Dao/service/action

2.1 引入jar文件

八、框架整合(三) - SSH 整合_第1张图片

2.2 代码

>>>>>> web.xml (配置struts核心过滤器、配置spring监听器)

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	
	<filter>
		<filter-name>struts2filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
	filter>
	<filter-mapping>
		<filter-name>struts2filter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	
	
	<context-param>
			<param-name>contextConfigLocationparam-name>
				
			
		
				<param-value>
							/WEB-INF/classes/bean-*.xml
				param-value>
			
			
	context-param>
	
	<listener>
			<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
	
web-app>
>>>>>> struts.xml (配置struts配置文件)


<struts>
    <package name="xxxx" extends="struts-default">
    	
    	
    	<action name="list" class="deptAction" method="sayHello">
    		<result name="list">/index.jspresult>
    	action>
    
    
    
    package> 
struts>
>>>>>> bean-*.xml (使用完整整合hibernate方式进行整合)

bean-base.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test">property>
		<property name="driverClass" value="com.mysql.jdbc.Driver">property>
		<property name="user" value="root">property>
		<property name="password" value="root">property>
		<property name="initialPoolSize" value="3">property>
		<property name="maxPoolSize" value="6">property>
		<property name="maxIdleTime" value="1000">property>
	bean>
    
  	
   	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource">property>
		
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
				<prop key="hibernate.show_sql">trueprop>
				<prop key="hibernate.hbm2ddl.auto">updateprop>
			props>
		property>
		
		
		
		
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:org/jsoft/entityvalue>
			list>
		property>
	bean>
	
    
      
    
    
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory">property>
    bean>
    
    
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
				<tx:method name="get*"  read-only="true"/>
				<tx:method name="find*" read-only="true"/>
				<tx:method name="*" read-only="false"/>
		tx:attributes>
	tx:advice>
	
    
	<aop:config>
		
		<aop:pointcut expression="execution(* org.jsoft.service.*.*(..))" id="pt"/>
			
		
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
	aop:config>
    
    
beans>

bean-dao.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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="deptDao"  class="org.jsoft.dao.DeptDao">
    	<property name="sessionFactory"  ref="sessionFactory">property>
    bean>
  
    
beans>

bean-service.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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="deptService"  class="org.jsoft.service.DeptService">
    	<property name="deptDao"  ref="deptDao">property>
    bean>
    
beans>

bean-controller.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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="deptAction" class="org.jsoft.action.DeptAction">
    	<property name="deptService"  ref="deptService">property>
    bean>
    
beans>
>>>>>> DeptEntity.hbm.xml



<hibernate-mapping package="org.jsoft.entity" >
	
	<class name="DeptEntity" table="dept">

		
		
		<id name="id" column="id" type="java.lang.String">id>
		
		
				
		<property name="name" column="name">property>
		
	class>

hibernate-mapping>

>>>>>> DeptEntity .java
public class DeptEntity {
     
	
	private String id;
	private String name;
	
	public String getId() {
     
		return id;
	}
	public void setId(String id) {
     
		this.id = id;
	}
	public String getName() {
     
		return name;
	}
	public void setName(String name) {
     
		this.name = name;
	}
	
	
	
}

>>>>>> DeptDao.java
public class DeptDao {
     
	
	private SessionFactory sessionFactory;

	
	public void setSessionFactory(SessionFactory sessionFactory) {
     
		this.sessionFactory = sessionFactory;
	}
	
	
	public List<DeptEntity> query() {
     
		Session session = sessionFactory.getCurrentSession();
		
		Query query = session.createQuery("from DeptEntity");
		List<DeptEntity> list = query.list();
		
		return list;
	}
}

>>>>>> DeptService .java
public class DeptService {
     
	
	private DeptDao  deptDao;

	public void setDeptDao(DeptDao deptDao) {
     
		this.deptDao = deptDao;
	}
	
	
	
	public List<DeptEntity> query() {
     
		List<DeptEntity> query = deptDao.query();
		return query;
		
	}
}

>>>>>> DeptAction .java
public class DeptAction extends ActionSupport{
     
	
	
	private DeptService  deptService;
	
	public void setDeptService(DeptService deptService) {
     
		this.deptService = deptService;
	}


	public String  sayHello() {
     
		
		List<DeptEntity> result = deptService.query();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("list", result);
		
		return "list";
	}
}

>>>>>> index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
	spring-struts-hibernate

	<table>
		
		<tr>
			<td>IDtd>
			<td>姓名td>
		<tr>
		
		<c:forEach items="${list}"  var="item" varStatus="index">
			<tr>
				<td>${item.id}td>
				<td>${item.name}td>
			tr>		
		c:forEach>
	
	table>
body>
html>

2.3 测试

	将项目部署到tomcat中,访问
			http://localhost:8080/my_spring_sturts_hibernate/list
	

八、框架整合(三) - SSH 整合_第2张图片

三、整合细节

3.1 jsp页面不能直接使用hibernate中的懒加载数据,如果需要使用,需要配置

3.1.1 jsp页面为什么不能直接使用hibernate中的懒加载数据
	SSH整合情况下,事务管理交给spring处理。
	事务作用于service层,当执行一个service成功,就意味着事务已经提交了。此时spring就会提交事务,关闭session。
	
	此时如果在jsp访问hibernate懒加载数据,肯定是报错的,
	因为,此时session已经关闭

八、框架整合(三) - SSH 整合_第3张图片

3.1.2 方式一:配置过滤器,使jsp可以直接访问懒加载数据
>>>>> 配置web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	
	
	<filter>
		<filter-name>OpenSessionInViewfilter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilterfilter-class>
	filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewfilter-name>
		<url-pattern>*.actionurl-pattern>
	filter-mapping>

	
	<filter>
		<filter-name>struts2filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
	filter>
	<filter-mapping>
		<filter-name>struts2filter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	
	
	<context-param>
			<param-name>contextConfigLocationparam-name>
				
			
		
				<param-value>
							/WEB-INF/classes/bean-*.xml
				param-value>
			
			
	context-param>
	
	<listener>
			<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
	
web-app>
3.1.3 方式二:lazy=false,关闭懒加载,可以使jsp可以直接访问懒加载数据
	
	集合标签(Set标签、List标签等都可以)可以配置懒加载:
			<set name="emps"  table="l_emp" lazy="false">
					<key column="deptId">key>
					
					<one-to-many class="Emp">one-to-many>
			set>		
	


	多对一标签可以配置懒加载:
		 <many-to-one name="dept"  column="deptId" class="Dept"  lazy="false">many-to-one>
	

你可能感兴趣的:(小白,-,WEB框架(高),-,Spring)