SSH框架整合

本人博客仅做自我学习用途(写个笔记),部分笔记手写,部分摘抄,如有冒犯,告知删除

Sring与Struts整和目的,让struts2的action由容器管理,从容器里拿到对象

Spring与Hibernate整和目的

管理sessionFactory实例(只需要一个,单例)

Spring   IOC容器 -->管理对象,管理单例对象,装配扫描注入对象 。。

              AOP面向切面编程---> 管理实务。。


struts2配置

1.web.xml

<span style="font-size:18px;">  	<!-- 配置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></span>

2.struts.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 配置为开发模式 -->
	<constant name="struts.devMode" value="true" />

	<!-- 拓展名配置为action -->
	<constant name="struts.action.extension" value="action" />

	<!-- 将主题配置为simple -->
	<constant name="struts.ui.theme" value="simple" />

	<package name="default" namespace="/" extends="struts-default">
		
		<!-- 配置测试struts的action,未于Spring整合,class属性写类的全名 -->
		<action name="test" class="cn.linyi.oa.test.TestAction" method="execute">
			<result name="success">/test.jsp</result>
		</action>
	</package>
</struts></span>

3.Jar包

4.struts2测试(jsp页面+Action类)

  test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>Struts2测试页面</title>

  
  <body>
    struts2配置成功 !<br>
  </body>
</html>

cn.linyi.oa.test.TestAction

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="html">package cn.linyi.oa.test;


import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
	@Override
	public String execute()throws Exception{
		System.out.println("-->TestAction.execute()");
		return "success";
	}
}


 URL输入http://localhost:8080/项目名/test.action(后缀名已设定,必须添加)

Spring配置
1.applicationContext.xml / bean.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 自动扫描与装配bean -->
	<context:component-scan base-package="cn.linyi.oa"></context:component-scan>
</beans>
2.对Action类修改,添加注解注入
package cn.linyi.oa.test;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionSupport;

<span style="color:#ff0000;">@Component
@Controller
@Service
@Repository</span>
public class TestAction extends ActionSupport {
	@Override
	public String execute()throws Exception{
		System.out.println("-->TestAction.execute()");
		return "success";
	}
}
3.spring测试类
package cn.linyi.oa.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

	private ApplicationContext ac =new  ClassPathXmlApplicationContext("applicationContext.xml");
	@Test
	public void testBean() throws Exception{
		TestAction testAction= (TestAction)ac.getBean("testAction");
		/*虽然spring定义了对cn.linyi.oa包的自动扫描与装配,但若TestAction没有注解注入,会导致输出testAction对象出错*/
		System.out.println("testAction:"+testAction);
		
	}
}
4.Spring Jar包+Junit4测试jar包
Struts与Spring整合
1.web.xml
  	<!-- 配置Spring的用于初始化容器对象的监听器 -->
	<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>
2.修改struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 配置为开发模式 -->
	<constant name="struts.devMode" value="true" />

	<!-- 拓展名配置为action -->
	<constant name="struts.action.extension" value="action" />

	<!-- 将主题配置为simple -->
	<constant name="struts.ui.theme" value="simple" />

	<package name="default" namespace="/" extends="struts-default">
		
		<!-- 配置测试struts的action,未于Spring整合,class属性写类的全名 -->
		<!-- 当striys2与spring整合后,class属性的属性值写bean的名称 -->
		<action name="test" <span style="color:#ff0000;">class="testAction"</span> method="execute">
			<result name="success">/test.jsp</result>
		</action>
	</package>
</struts>
3.applicationContext.xml文件如上,不修改
4.SpringTest.java类如上,不修改
5.整合struts-spring-plugin-2.1.8.1.jar

URL输入http://localhost:8080/项目名/test.action

Spring整合Hibernate
1.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 自动扫描与装配bean -->
	<context:component-scan base-package="cn.linyi.oa"></context:component-scan>

	<!-- 导入外部的properties文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 指定hibernate的配置文件位置 -->
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<!-- 配置c3p0数据库连接池 -->
		<property name="dataSource" ref="dataSource">
		</property>
	</bean>	
		
		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
				<!-- 数据连接信息 -->
				<property name="jdbcUrl" value="${jdbcUrl}"></property>
				<property name="driverClass" value="${driverClass}"></property>
				<property name="user" value="${user}"></property>
				<property name="password" value="${password}"></property>
				<!-- 其他配置 -->
				<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
				<property name="initialPoolSize" value="3"></property>
				<!--连接池中保留的最小连接数。Default: 3 -->
				<property name="minPoolSize" value="3"></property>
				<!--连接池中保留的最大连接数。Default: 15 -->
				<property name="maxPoolSize" value="5"></property>
				<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
				<property name="acquireIncrement" value="3"></property>
				<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
				<property name="maxStatements" value="8"></property>
				<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
				<property name="maxStatementsPerConnection" value="5"></property>
				<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
				<property name="maxIdleTime" value="1800"></property>
			</bean>
	
	<!-- 配置声明式事务管理(采用注解的方式) -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>
	

</beans>
2.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>

	<!-- 1,数据库连接信息+方言设置 -->
	<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
	
	<!-- 屏蔽hibernate自带的数据库连接池,使用c3p0 --><!-- 
	<property name="connection.url">jdbc:mysql:///oa</property>
	<property name="connection.driver_class">com.jdbc.mysql.Driver</property>
	<property name="connection.username">root</property>
	<property name="connection.password">root</property>
	 -->
	<!-- 2,其他配置 (自动建表)(显示sql语句)(配置二级缓存)-->
	<property name="show_sql">true</property>
	<property name="hbm2ddl.auto">update</property>
	
	<!-- 3,导入映射文件 (空值需屏蔽掉,否则出错)-->
<!-- 
	<mapping resource=""/>
 -->
</session-factory>

</hibernate-configuration>
3.测试类(测试sessionFactory)
package cn.linyi.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

	private ApplicationContext ac =new  ClassPathXmlApplicationContext("applicationContext.xml");
	@Test
	public void testBean() throws Exception{
		TestAction testAction= (TestAction)ac.getBean("testAction");
		/*虽然spring定义了对cn.linyi.oa包的自动扫描与装配,但若TestAction没有注解注入,会导致输出testAction对象出错*/
		System.out.println("testAction:"+testAction);
		
	}
<span style="color:#ff0000;">	// 测试SessionFactory
	@Test
	public void testSessionFactory() throws Exception {
		SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
		System.out.println("sessionFactory:"+sessionFactory);
	}
</span>
}
4.web.xml添加spring的监听器如上

测试事务
package cn.linyi.oa.test;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.linyi.oa.domain.User;
//想要注入的话,当前对象必须在容器里
@Service
public class TestService {
	//通过sessionFactory得到session
	@Resource
	private SessionFactory sessionFactory;

	@Transactional
	public void saveTwoUsers() {
        Session session=sessionFactory.getCurrentSession();
        session.save(new User());
        //int a=1/0;//控制抛异常,会回滚
        session.save(new User());
        
	}
}
测试时,自动建表(在数据库建好的情况下)
package cn.linyi.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

	private ApplicationContext ac =new  ClassPathXmlApplicationContext("applicationContext.xml");
	//	测试spring配置是否成功
	@Test
	public void testBean() throws Exception{
		TestAction testAction= (TestAction)ac.getBean("testAction");
		/*虽然spring定义了对cn.linyi.oa包的自动扫描与装配,但若TestAction没有注解注入,会导致输出testAction对象出错*/
		System.out.println("testAction:"+testAction);
		
	}
	// 测试SessionFactory(测试Spring与Hibernate整合情况)
	@Test
	public void testSessionFactory() throws Exception {
		SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
		System.out.println("sessionFactory:"+sessionFactory);
	}
<span style="color:#ff0000;">	//测试事务(测试Spring与Hibernate整合情况)
	@Test
	public void testTransaction() throws Exception{
		TestService testService=(TestService) ac.getBean("testService");
		testService.saveTwoUsers();
		
	}</span>
	
}




你可能感兴趣的:(SSH框架整合)