1.web.xml配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app 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">
- <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <!-- 对Spring容器进行实例化,并把实例存放在application的属性里 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置字符编码过滤器(解决乱码问题) -->
- <filter>
- <filter-name>encoding</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 解决因session关闭而导致的延迟加载例外的问题 -->
- <filter>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>OpenSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 配置Struts1的中心控制器ActionServlet-->
- <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>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>struts</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
2.struts-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!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>
- <!-- 配置formbean -->
- <form-beans>
- <form-bean name="personForm" type="cn.itcast.formbean.PersonForm"/>
- </form-beans>
- <!-- 配置全局跳转 -->
- <global-forwards>
- <forward name="message" path="/WEB-INF/page/message.jsp"/>
- </global-forwards>
- <!-- Action映射配置,这里Action交给Spring去管理 -->
- <action-mappings>
- <action path="/personlist" validate="false">
- <forward name="list" path="/WEB-INF/page/persons.jsp"/>
- </action>
- <action path="/personmanage" name="personForm" scope="request" validate="false" parameter="method">
- <forward name="add" path="/WEB-INF/page/addperson.jsp"/>
- </action>
- </action-mappings>
- <!-- 将请求交给Spring容器处理 -->
- <controller>
- <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
- </controller>
- </struts-config>
3.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">
- <context:annotation-config/>
- <!-- 配置数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/xwuxintwo?useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="1"/>
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="500"/>
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="2"/>
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请一些连接,以避免洪峰来时再申请而造成的性能开销 -->
- <property name="minIdle" value="1"/>
- </bean>
- <!-- 使用Spring提供的sessionFactory 指定它要使用的数据源以及数据库相关信息 -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- <value>cn/xwuxin/bean/Person.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <value>
- <!-- 方言 -->
- hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
- <!-- 这里第一次使用时可以把update改为create让其自动生成表-->
- hibernate.hbm2ddl.auto=update
- <!-- 配置是否显示sql语句 -->
- hibernate.show_sql=false
- <!-- 是否格式化sql语句 -->
- hibernate.format_sql=false
- <!-- 是否使用二级缓存 -->
- hibernate.cache.use_second_level_cache=true
- <!-- 是否使用查询缓存 -->
- hibernate.cache.use_query_cache=false
- <!-- 设置二级缓存的提供者 (OSCache或EHCache)-->
- hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
- </value>
- </property>
- </bean>
- <!-- 配置事务管理器指定其作用的sessionFactory 这样就把事务交给Spring去处理,减轻程序员负担-->
- <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <!-- aop的配置 -->
- <aop:config>
- <aop:pointcut id="tran" expression="execution(* cn.xwuxin.service..*.*(..))"/>
- <aop:advisor advice-ref="txAdvice" pointcut-ref="tran"/>
- </aop:config>
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
- <tx:method name="*"/>
- </tx:attributes>
- </tx:advice>
- <!-- 其他要交给Spring管理的bean -->
- <bean id="personService" class="cn.xwuxin.service.impl.PersonServiceBean"/>
- <bean name="/personlist" class="cn.xwuxin.action.PersonListAction"/>
- <bean name="/personmanage" class="cn.xwuxin.action.PersonManageAction"/>
- </beans>
4.ehcache.xml配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- defaultCache节点为缺省的缓存策略
- maxElementsInMemory 内存中最大允许存在的对象数量
- eternal 设置缓存中的对象是否永远不过期
- overflowToDisk 把溢出的对象存放到硬盘上
- timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉
- timeToLiveSeconds 指定缓存对象总的存活时间
- diskPersistent 当jvm结束是是否持久化对象
- diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间
- -->
- <ehcache>
- <diskStore path="D:/cache"/>
- <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
- timeToIdleSeconds="120"
- timeToLiveSeconds="180"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="60"/>
- <cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
- overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>
- </ehcache>
5.Person.hbm.xml配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="cn.itcast.bean">
- <class name="Person" table="person">
- <cache usage="read-write" region="cn.itcast.bean.Person"/>
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="name" length="10" not-null="true"/>
- </class>
- </hibernate-mapping>