SSH环境通用整合配置(Spring2.5+Hibernate3.3+Struts1.3)

 SSH整合通用

准备:

需要:Spring2.5+Hibernate3.3+Struts1.3

❶hibernate核心安装包下的:

hibernate3.jar

lib\required\*.jar

lib\optional\ehcache-1.2.3.jar

hibernate注解安装包下的

lib\test\slf4j-log4j12.jar

❷Spring安装包下的

dist\spring.jar

dist\modules\spring-webmvc-struts.jar

lib\jakarta-commons\commons-logging.jar、commons-dbcp.jar、commons-pool.jar

lib\aspectj\aspectjweaver.jar、aspectjrt.jar

lib\cglib\cglib-nodep-2.1_3.jar

lib\j2ee\common-annotations.jar

lib\log4j\log4j-1.2.15.jar

❸Struts下载struts-1.3.8-lib.zip,需要使用到解压目录下的所有jar,建议把jstl-1.0.2.jar和standard-1.0.2.jar更换为1.1版本。Spring中已经存在一个antlr-2.7.6.jar,所以把struts中的antlr-2.7.2.jar删除,避免jar冲突。

❹数据库驱动jar 

开始配置:

一、web配置

web.xml

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


  <display-name />
  <!-- 路径为目录时,默认的读取的文件,默认为index.jsp -->
  <welcome-file-list>
    <welcome-file>_.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- SSH整合必须 -->
  <!-- 整合Struts1.x -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value><!-- 引用Struts1.x的配置文件 -->
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  
   <!-- Struts1.x拦截特定后缀的请求路径,进行action处理 -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <!-- end-整合Struts1.x -->
  
   <!-- SSH整合必须 -->
   <!-- 整合Spring2.5 -->
   <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value><!-- 引用Sping的配置文件 -->
   </context-param>
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
  
 	<!-- OpenSessionInViewFilter,解决Spring和Hibernate整合后,读取数据库时Hibernate的session已经关闭的问题 -->
	<filter>
		<filter-name>hibernateFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>hibernateFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- end-整合Spring2.5 -->
	
	<!-- 自定义的拦截器,拦截器定义越靠前,优先级越高 -->
	<!-- 使用Spring提供的编码转换,解决乱码问题 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>GB18030</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 分页拦截器,用以读取当前页的显示行数和首行的偏移量 -->
	<filter>
		<filter-name>PaginateFilter</filter-name>
		<filter-class>com.oa.rs.PaginateFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>PaginateFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	
	<!-- 整合Javascript类库DWR -->
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
	    <servlet-name>dwr-invoker</servlet-name>
	    <url-pattern>/dwr/*</url-pattern>
	 </servlet-mapping>
	  
  
</web-app>

二、Struts配置

struts-config.xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

  <!-- ActionForm类,包含request的参数变量 -->
  <form-beans >
     <form-bean name="userForm" type="com.oa.struts.action.form.UserActionForm" />
     <form-bean name="orgForm" type="com.oa.struts.action.form.OrgActionForm" />
     <form-bean name="personForm" type="com.oa.struts.action.form.PersonActionForm" />
     <form-bean name="moduleForm" type="com.oa.struts.action.form.ModuleActionForm" />
   </form-beans>
	
  <!-- 全局异常 -->
  <global-exceptions />
  
  <!-- 全局转向 -->
  <global-forwards >
  	<forward name="public_add_success" path="/common/public_add_success.jsp"></forward>
  	<forward name="public_update_success" path="/common/public_update_success.jsp"></forward>
  	<forward name="public_delete_success" path="/common/public_delete_success.jsp"></forward>
  	<forward name="login" path="/login.jsp"></forward>
  </global-forwards>
  
  <!-- action类关联配置 -->
  <action-mappings >
  	<!-- name:  所引用ActionForm,
  		 parameter:   方法标识名,标识名对应的值是Action类所使用的方法(函数)名,	如parameter="method",请求参数method=addUser,即使用addUser方法
  		 path:   关联请求的url,如user.do,
  		 scope:    actionForm的生存范围 
  		 type:   关联的Action类,和Spring整合后,则统一使用代理类org.springframework.web.struts.DelegatingActionProxy,使由Spring管理Action类
  		 forward name,path:  转向的路径标识名和路径
  	-->
    <action
	      name="userForm"
	      parameter="method"
	      path="/user"
	      scope="request"
	      type="org.springframework.web.struts.DelegatingActionProxy">
	      	<forward name="index"  path="/user/index.jsp"></forward>
	      	<forward name="add"  path="/user/add_input.jsp"></forward>
	      	<forward name="update"  path="/user/update_input.jsp"></forward>
	      	<forward name="user_role_list"  path="/user/user_role_list.jsp"></forward>
	      	<forward name="user_role_input"  path="/user/user_role_input.jsp"></forward>
      </action>
      
      <action
	      name="loginForm"
	      parameter="method"
	      path="/login"
	      scope="request"
	      type="org.springframework.web.struts.DelegatingActionProxy">
	      	<forward name="index"  path="/success.jsp"></forward>
      </action>
      
       <action
	      name="orgForm"
	      parameter="method"
	      path="/org"
	      scope="request"
	      type="org.springframework.web.struts.DelegatingActionProxy">
	      	<forward name="index"  path="/org/index.jsp"></forward>
	      	<forward name="add_input"  path="/org/add_input.jsp"></forward>
	      	<forward name="update_input"  path="/org/update_input.jsp"></forward>
	      	<forward name="success"  path="/org.do?method=index"></forward>
	      	<forward name="select"  path="/org/select_org.jsp"></forward>
      </action>
      
      <action
	      name="personForm"
	      parameter="method"
	      path="/person"
	      scope="request"
	      type="org.springframework.web.struts.DelegatingActionProxy">
	      	<forward name="index"  path="/person/index.jsp"></forward>
	      	<forward name="add"  path="/person/add_input.jsp"></forward>
	      	<forward name="update"  path="/person/update_input.jsp"></forward>
      </action>
      
      <action
	      name="moduleForm"
	      parameter="method"
	      path="/module"
	      scope="request"
	      type="org.springframework.web.struts.DelegatingActionProxy">
	      	<forward name="index"  path="/module/index.jsp"></forward>
	      	<forward name="add"  path="/module/add_input.jsp"></forward>
	      	<forward name="update"  path="/module/update_input.jsp"></forward>
      </action>
      
  </action-mappings>
  <!-- 关联资源文件 -->
  <message-resources parameter="com.oa.rs.resources.properties" />
</struts-config>

 三、Spring配置

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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


	<import resource="applicationContext-config.xml" />
	<import resource="applicationContext-action.xml" />
	<import resource="applicationContext-implement.xml" />
</beans>

1)数据源、事务管理及其他集成配置

applicationContext-config.xml

<?xml version="1.0" encoding="GBK"?>

<!-- config beans -->

<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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">



<!-- sessionFactory配置,有多种方法,这里采用引用Hiberate的配置文件方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="configLocation"  >
		<value>classpath:hibernate.cfg.xml</value>
	</property>
</bean>
<!-- 事务管理依赖注入 -->
<bean id="transationManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务管理配置 -->
<tx:advice id="txAdvice" transaction-manager="transationManager">
	<tx:attributes>
		<tx:method name="*" propagation="REQUIRED"/>
		<!--<tx:method name="add*" propagation="REQUIRED"/>
		<tx:method name="del*" propagation="REQUIRED"/>
		<tx:method name="update*" propagation="REQUIRED"/>
		<tx:method name="*" read-only="true"/>
	--></tx:attributes>
</tx:advice>
<!-- 事务管理切入点 -->
<aop:config>
	<aop:pointcut expression="execution (* com.oa.manager.*.*(..))"  id="allManagerMethod"/>
	<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>

<!-- jbpm工作流配置 -->
	<bean id="jbpmConfiguration" class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
		<property name="configuration" value="classpath:jbpm.cfg.xml"></property>
	</bean>

</beans>

  

2)继承了HibernateTemplate的*Impl实现类,注入包含了数据源的sessionFactory,使可以进行数据库连接和操作

applicationContext-implement.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- Implement IoC -->

<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"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


<!--user beans -->
<bean id="UserManagerImpl" class="com.oa.manager.impl.UserManagerImpl">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="OrgManagerImpl" class="com.oa.manager.impl.OrgManagerImpl">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="PersonManagerImpl" class="com.oa.manager.impl.PersonManagerImpl">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="ModuleManagerImpl" class="com.oa.manager.impl.ModuleManagerImpl">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="RoleManagerImpl" class="com.oa.manager.impl.RoleManagerImpl">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

3)action类中所定义的一些业务接口类实现依赖注入

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- action beans -->
<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"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- action beans -->
<bean name="/user" class="com.oa.struts.action.UserAction">
	<property name="iu" ref="UserManagerImpl"/>
	<property name="ip" ref="PersonManagerImpl"/>
	<property name="ir" ref="RoleManagerImpl"/>
</bean>
<bean name="/login" class="com.oa.struts.action.LoginAction">
	<property name="iu" ref="UserManagerImpl"/>
</bean>
<bean name="/org" class="com.oa.struts.action.OrgAction">
	<property name="io" ref="OrgManagerImpl"/>
</bean>
<bean name="/person" class="com.oa.struts.action.PersonAction">
	<property name="ip" ref="PersonManagerImpl"/>
</bean>
<bean name="/module" class="com.oa.struts.action.ModuleAction">
	<property name="im" ref="ModuleManagerImpl"/>
</bean>
</beans>

 四、Hibernate配置

Hibernate配置:

<?xml version='1.0' encoding='GBK'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

	<session-factory>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 连接mysql的jdbc驱动 -->
		<!-- <property name="connection.url">jdbc:mysql://192.168.1.19:3306/oa2?userUnicode=true&amp;characterEncoding=utf-8</property> -->
		<property name="connection.url">jdbc:mysql://192.168.1.28:3306/new_oa2</property> <!-- 连接mysql的url -->
		<property name="connection.username">root</property> <!-- 连接mysql的用户 -->
		<property name="connection.password">123qwe</property> <!-- 连接mysql的密码 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 操作时的方言 -->
		<property name="show_sql">true</property> <!-- 显示sql操作语句 -->
		<property name="hbm2ddl.auto">update</property> <!-- 采用更新表方式,不存在表示创建,存在表时更新 -->
		
		<!-- 数据池方式暂略 -->
		<!-- ... -->
		
		<!-- 二级缓存 -->
		<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
		
		<!-- POJO映射 -->
		<mapping resource="com/oa/model/User.hbm.xml" />
		<mapping resource="com/oa/model/Organization.hbm.xml" />
		<mapping resource="com/oa/model/Person.hbm.xml" />
		<mapping resource="com/oa/model/Module.hbm.xml" />
		
	</session-factory>

</hibernate-configuration>

  

相关推荐