struts1+spring+JPA环境搭建及配置

时间:2022-05-06 20:36:21

 

Spring2.5+JPA+Struts1.3整合开发  这里JPA的实现采用hibernate,需要使用到下面的jar文件 Hiberante核心包(8个文件) hibernate-distribution-3.3.1.GA --------------------------------------------- hibernate3.jar lib/bytecode/cglib/hibernate-cglib-repack-2.1_3.jar lib/required/*.jar Hiberante注解包(3个文件)hibernate-annotations-3.4.0.GA ------------------------------------------------------------------------------------ hibernate-annotations.jar lib/ejb3-persistence.jarhibernate-commons-annotations.jar Hibernate针对JPA的实现包(3个文件)hibernate-entitymanager-3.4.0.GA ------------------------------------------------------------------------------------------------------ hibernate-entitymanager.jar lib/test/log4j.jarslf4j-log4j12.jar  Spring2.5struts1.3需要的jar Spring安装包下的 dist/spring.jar dist/modules/spring-webmvc-struts.jar lib/jakarta-commons/commons-logging.jarcommons-dbcp.jarcommons-pool.jar lib/aspectj/aspectjweaver.jaraspectjrt.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.jarstandard-1.0.2.jar更换为1.1版本。Spring中已经存在一个antlr-2.7.6.jar,所以把struts中的antlr-2.7.2.jar删除,避免jar冲突。  Spring2.5+JPA+Struts1.3整合开发 JPA规范要求在类路径的META-INF目录下放置persistence.xml,文件的名称是固定的,配置模版如下: <?xml version="1.0"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">   <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">       <properties>          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>          <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>          <property name="hibernate.connection.username" value="root"/>          <property name="hibernate.connection.password" value="123456"/>          <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>          <property name="hibernate.max_fetch_depth" value="3"/>          <property name="hibernate.hbm2ddl.auto" value="update"/>       </properties>   </persistence-unit> </persistence>  spring中配置EntityManagerFactory <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">       <property name="persistenceUnitName" value="itcast"/>   </bean>   <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">   <property name="entityManagerFactory" ref="entityManagerFactory"/>   </bean>  <tx:annotation-driven transaction-manager="txManager"/>  使用spring解决JPAEntityManager关闭导致的延迟加载例外问题。  <filter>         <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>         <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>         <init-param>    <!-- 指定org.springframework.orm.jpa.LocalEntityManagerFactoryBeanspring配置文件中的名称,默认值为entityManagerFactory.如果LocalEntityManagerFactoryBeanspring中的名称不是entityManagerFactory,该参数一定要指定,否则会出现找不到entityManagerFactory的例外 -->             <param-name>entityManagerFactoryBeanName</param-name>             <param-value>entityManagerFactory</param-value>       </init-param> </filter> beans.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-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/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">
 <aop:aspectj-autoproxy/>
 <context:component-scan base-package="cn.itcast"/> 
    <!--  使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
    需要在tomcat作一些特殊配置.具体参考手册
    注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     <property name="driverClassName" value="${driverClassName}"/>
     <property name="url" value="${url}"/>
     <property name="username" value="${username}"/>
     <property name="password" value="${password}"/>
     连接池启动时的初始值
     <property name="initialSize" value="${initialSize}"/>
     连接池的最大值
     <property name="maxActive" value="${maxActive}"/>
      最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止
     <property name="maxIdle" value="${maxIdle}"/>
      最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请
     <property name="minIdle" value="${minIdle}"/>
   </bean> 
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
   <property name="loadTimeWeaver">
           <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
 </bean>
 -->
   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
   <property name="persistenceUnitName" value="itcast"/>
   </bean>  
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
   </bean>
  
   <tx:annotation-driven transaction-manager="transactionManager"/>
  
   <bean id="compass" class="org.compass.spring.LocalCompassBean">
  <property name="classMappings">
   <list>
    <value>cn.itcast.bean.product.ProductInfo</value>
    <value>cn.itcast.bean.product.Brand</value>
    <value>cn.itcast.bean.product.ProductStyle</value>
    <value>cn.itcast.bean.product.ProductType</value>
   </list>
  </property>
  <property name="compassSettings">
   <props>
    <!-- 分词器 -->
    <prop key="compass.engine.analyzer.default.type">net.paoding.analysis.analyzer.PaodingAnalyzer</prop>
    <prop key="compass.engine.connection">file://d:/index</prop>
    <!-- 在内存中建立索引
    <prop key="compass.engine.connection">ram://index</prop>
    -->
    <prop key="compass.engine.highlighter.default.formatter.simple.pre"><![CDATA[<font color='red'>]]></prop>
    <prop key="compass.engine.highlighter.default.formatter.simple.post"><![CDATA[</font>]]></prop>
    <prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>
   </props>
  </property>
  <property name="transactionManager" ref="transactionManager" />
 </bean>
 <!-- 自动完成 索引的添加/更新/删除操作-->
 <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop">
  <property name="compass" ref="compass" />
  <property name="gpsDevices">
   <list>
    <!-- 设置JPA GPS驱动 -->
    <bean class="org.compass.gps.device.jpa.JpaGpsDevice">
      <property name="name" value="jpaDevice" />
      <property name="entityManagerFactory" ref="entityManagerFactory" />
      <property name="injectEntityLifecycleListener" value="true"/>
    </bean>
   </list>
  </property>
 </bean>
</beans>
persistence.xml<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
  <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
 <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123456"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.jdbc.fetch_size" value="18"/>
      <property name="hibernate.jdbc.batch_size" value="10"/>
      <property name="hibernate.show_sql" value="false"/>
      <property name="hibernate.format_sql" value="false"/>
      </properties>
  </persistence-unit>
</persistence>
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>babasport</display-name>
    <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:beans.xml</param-value>
 </context-param>
 <!-- 对Spring容器进行实例化 -->
 <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <filter>
      <filter-name>CacheFilter</filter-name>
      <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
      <init-param>
          <param-name>time</param-name>
          <param-value>60</param-value>
      </init-param>
      <init-param>
          <param-name>scope</param-name>
          <param-value>application</param-value>
      </init-param>
 </filter>
 <filter-mapping>
      <filter-name>CacheFilter</filter-name>
      <url-pattern>/product/list/*</url-pattern>
 </filter-mapping>
    <filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
     <init-param>
     <!-- 指定org.springframework.orm.jpa.LocalEntityManagerFactoryBean在spring配置文件中的名称,默认值为entityManagerFactory
     如果LocalEntityManagerFactoryBean在spring中的名称不是entityManagerFactory,该参数一定要指定,否则会出现找不到entityManagerFactory的例外 -->
   <param-name>entityManagerFactoryBeanName</param-name>
   <param-value>entityManagerFactory</param-value>
  </init-param> 
    </filter>
    <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 <filter>
        <filter-name>codeFilter</filter-name>
        <filter-class>cn.itcast.web.filter.SetCodeFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>codeFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>codeFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
 <filter>
        <filter-name>LogonValidateFilter</filter-name>
        <filter-class>cn.itcast.web.filter.LogonValidateFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LogonValidateFilter</filter-name>
        <url-pattern>/customer/*</url-pattern>
    </filter-mapping>  
 <filter>
        <filter-name>BuyCartIsEmptyFilter</filter-name>
        <filter-class>cn.itcast.web.filter.BuyCartIsEmptyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>BuyCartIsEmptyFilter</filter-name>
        <url-pattern>/customer/shopping/*</url-pattern>
    </filter-mapping> 
 <filter>
        <filter-name>EmployeeLogonValidaterFilter</filter-name>
        <filter-class>cn.itcast.web.filter.EmployeeLogonValidaterFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>EmployeeLogonValidaterFilter</filter-name>
        <url-pattern>/control/*</url-pattern>
    </filter-mapping>  
 <servlet>
  <servlet-name>struts</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>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>struts</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>