十六 Spring2.5+Hibernate3.3+Struts1.3整合开发
整合这几个框架,并不是一下子全部配置好的,一般来说先配置spring,然后整合hibernate,最后加入struts。
第一步 引入jar
引入spring的jar
spring的核心jar
dist\spring.jar
//整合struts1用到的jar
dist\modules\spring-webmvc-struts.jar
lib\jakarta-commons\commons-logging.jar、
//数据源支持的jar
commons-dbcp.jar、commons-pool.jar
//aop支持的jar
lib\aspectj\aspectjweaver.jar、aspectjrt.jar
lib\cglib\cglib-nodep-2.1_3.jar
//注解用到的jar
lib\j2ee\common-annotations.jar
//打印日志用到的jar
lib\log4j\log4j-1.2.15.jar
2 引入hibernate3的jar包
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/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!--配置sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<!--配置事务管理器容器-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启动事务注解管理方式 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!--配置bean-->
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
<bean id="personList" class="cn.itcast.web.PersonAction"/>
</beans>
关键的service
package cn.itcast.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;
//将服务纳入到spring的事务管理当中
@Transactional
public class PersonServiceBean implements PersonService {
//将sessionFactory依赖注入到当前的类中 作为一个属性存在 Resource注解 首先是从名称开始寻找 这个名称就是
//当前定义的这个属性,看这个属性是否在beans.xml中是否存在
@Resource private SessionFactory sessionFactory;
public void save(Person person){
sessionFactory.getCurrentSession().persist(person);
}
public void update(Person person){
//sessionFactory.getCurrentSession()这是获取被spring管理的Session对象 不能是openSession()
// 因为这样获取的Session是不被spring所管理的
sessionFactory.getCurrentSession().merge(person);
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Person getPerson(Integer personid){
return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);
}
public void delete(Integer personid){
//load()方法不需要装配对象 get要装配对象 封装过程
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(Person.class, personid));
}
//这一步是获取的方法 所以不需要开启事务 并且设置为只读
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("from Person").list();
}
}
到这一步最好是测试一下 看spring跟hibernate是否集成成功
package junit.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;
public class PersonServiceTest {
private static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
personService = (PersonService)applicationContext.getBean("personService");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
@Test
public void testSave() {
personService.save(new Person("小张"));
}
@Test
public void testUpdate() {
Person person = personService.getPerson(1);
//....
person.setName("小丽");
personService.update(person);
}
@Test
public void testGetPerson() {
Person person = personService.getPerson(1);
System.out.println(person.getName());
try {
System.out.println("请关闭数据库");
Thread.sleep(1000*15);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第二次开始获取");
person = personService.getPerson(1);
System.out.println(person.getName());
}
@Test
public void testDelete() {
personService.delete(1);
}
@Test
public void testGetPersons() {
List<Person> persons = personService.getPersons();
for(Person person : persons){
System.out.println(person.getName());
}
}
}
现在我们来集成struts1
下载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冲突。
然后把lib下的jar全部加入到工程中
添加struts-config.xml配置文件
在web.xml对spring和struts进行配置
<?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:前缀指定从类路径下寻找 -->
<!--作用:让web容器去初始化spring-->
<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>
<!-- 配置struts的web框架-->
<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>
现在我们来新建一个action
package cn.itcast.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.itcast.service.PersonService;
public class PersonAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//得到spring容器实例
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext
(this.getServlet().getServletContext());
PersonService personservice=(PersonService)ctx.getBean("personService");
request.setAttribute("persons", personservice.getPersons());
return mapping.findForward("list");
}
}
配置action struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<action-mappings>
<action path="/person/list" type="cn.itcast.web.PersonAction" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
</action-mappings>
</struts-config>
测试下是否通过即可
如果action没有交给spring管理时,我们通过下面语句获取spring容器实例
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
把action交给spring管理后,我们可以使用依赖注入在action中注入业务层的bean。确保action的path属性值与bean的名称相同。
<action path="/person/list" ...>
</action>
Spring 配置:
<bean name="/person/list" class="cn.itcast.web.action.PersonAction"/>
在struts配置文件中添加进spring的请求控制器,该请法语控制器会先根据action的path属性值到spring容器中寻找跟该属性值同名的bean。如果寻找到即使用该bean处理用户请求
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
具体步骤如下:
将action交给Spring进行管理,并把相应的service服务依赖注入到Action中
第一步:将Action注册到spring 中 并设置其名称 名称与struts中的path名称一致
spring中的配置
<bean name="/person/list" class="cn.itcast.web.PersonAction"/>
struts中的配置
<action-mappings>
<action path="/person/list" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
</action-mappings>
发现action少了type了呢,这是为下一步做好配置,因为交给spring去管理后,struts中的spring
控制器会在spring中寻找spring中的配置的bean名称为path名称的bean
第二步 在struts-config.xml中加入spring的控制器 用于管理action 这时action就交给了spring进行管理了
<!-- 配置spring的控制器 -->
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
第三步:将服务依赖注入到action中 采用注解方式Resource
现在来看action
package cn.itcast.web;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.itcast.service.PersonService;
public class PersonAction extends Action {
@Resource PersonService personservice;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//得到spring容器实例
//WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext
//(this.getServlet().getServletContext());
//PersonService personservice=(PersonService)ctx.getBean("personService");
request.setAttribute("persons", personservice.getPersons());
return mapping.findForward("list");
}
}
分析:发现不需要WebApplicationContext来获得spring的容器实例了
这时ssh框架就已经完成了
现在我们还要去配置二级缓存
第一步 加入缓存的jar包 当前我们使用ehcache这个第三方框架 lib\optional\ehcache-1.2.3.jar
第二步 配置 在spring中的sessionFactory去配置
<!-- 配置二级缓存 -->
hibernate.cache.use_second_level_cache=true
<!-- 是否使用查询缓存 -->
hibernate.cache.use_query_cache=false
<!-- 配置使用缓存的驱动类 -->
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
第三步 在src目录下加入缓存第三方框架配置文件 Ehcache默认的配置文件ehcache.xml(放在类路径下)
<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>
defaultCache节点为缺省的缓存策略
maxElementsInMemory 内存中最大允许存在的对象数量
eternal 设置缓存中的对象是否永远不过期
overflowToDisk 把溢出的对象存放到硬盘上
timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉
timeToLiveSeconds 指定缓存对象总的存活时间
diskPersistent 当jvm结束是是否持久化对象
diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间
<diskStore path="D:\cache"/> 表示的是缓存存放的硬盘路径
第四步:为指定的类注册缓存,并指定其缓存策略 缓存的区域名 这个区域是用来配置缓存的对象,这个对象就是当前配置的这个
实体的对象数据
方法:在实体bean的xml映射文件中配置 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-only" region="cn.itcast.bean."/>
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="10" not-null="true"/>
</class>
</hibernate-mapping>
现在我们来对指定后的缓存区域进行缓存的配置
配置方法 在缓存框架的配置文件(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"/>
<!--给缓存区域是cn.itcast.bean.Person的指定特殊缓存配置,如果不指定将运用上述公共的缓存配置-->
<cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>
</ehcache>
现在缓存的配置就已经好了
现在我们来测试 怎么测试呢
查询
--关闭---
查询
如果能再第二次查询能过查询到数据
就说明缓存配置成功
当前我们只能对单个的数据进行查询配置成功
如果对list的集合类型有多条数据的查询当前的配置是不能成功的
只能对单条的数据进行查询,能运用到缓存
如果要findall() list() Iterator() createCriteria() createQuery()等方法获得数据
结果集得话,需要设置hibernate.cache.use_query true才行
并且需要查询缓存,还需要在使用Query或者criteria()时设置其setCacheable(true)属性
现在来看spring配置文件
<?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/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
<!-- 配置二级缓存 -->
hibernate.cache.use_second_level_cache=true
<!-- 是否使用查询缓存 当前配置上查询缓存 ,但是查询
缓存的命中率并不是很高,所以没有必要为其配置 当前是设为true
是配置上查询缓存
-->
hibernate.cache.use_query_cache=true
<!-- 配置使用缓存的驱动类 -->
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启动事务注解管理方式 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
<bean name="/person/list" class="cn.itcast.web.PersonAction"/>
</beans>
现在来看DAO的service类
//这一步是获取的方法 所以不需要开启事务 并且设置为只读
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("from Person").setCacheable(true).list();
}
分析:sessionFactory.getCurrentSession().createQuery("from Person").setCacheable(true).list();
当前是不是配置上了,推荐查询缓存不要去做配置
测试类
@Test
public void testGetPersons() {
List<Person> persons = personService.getPersons();
for(Person person : persons){
System.out.println(person.getName());
}
try {
System.out.println("请关闭数据库");
Thread.sleep(1000*15);
} catch (InterruptedException e) {
e.printStackTrace();
}
persons = personService.getPersons();
for(Person person : persons){
System.out.println(person.getName());
}
}
测试成功
注意:二级缓存是在SessionFactory中生效的,如果SessionFactory关闭后,二级缓存的
数据也随之在内存中结束,因为缓存是基于一个对象的,一级缓存基于Session对象
二级缓存基于SessionFactory对象,如果SessionFactory关闭,那么跟着它的缓存的生命
也将结束 所以这个测试的方法,只能运行一次,如果第二次在关闭数据库的情况下
缓存是不起作用的,会报异常
好了 现在缓存已经配置好了
我们现在来建一个action
package cn.itcast.web;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import cn.itcast.bean.Person;
import cn.itcast.formbean.PersonForm;
import cn.itcast.service.PersonService;
/**
Resource是注解 ,用来依赖注入到这个Action 作为一个属性
*/
@Resource PersonService personservice;
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
PersonForm personform=(PersonForm)form;
personservice.save(new Person(personform.getName()));
request.setAttribute("message", "保存成功");
return mapping.findForward("message");
}
}
配置
<form-beans>
<form-bean name="PersonForm" type="cn.itcast.formbean.PersonForm"/>
</form-beans >
<global-forwards>
<forward name="message" path="/WEB-INF/page/message.jsp"></forward>
</global-forwards>
<action path="/person/pmessage" validate="false" parameter="method"
scope="request" name="PersonForm"></action>
将当前的action交给spring管理
<bean name="/person/pmessage" class="cn.itcast.web.PersonMessageAction"/>
新建 index.jsp
引入 struts标签
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
在页面中添加一个表单
<html:form action="/person/pmessage" method="post">
<html:text property="name"/>
<html:hidden property="method" value="add"/>
<html:submit value="提交 "/>
</html:form>
现在来建一个message.jsp
<body>
${message }
</body>
现在在浏览器输入测试
http://localhost:8808/SSH/
Spring2.5+Hibernate3.3+Struts1.3的数据录入乱码解决
在web.xml中加入spring的乱码解决struts1的问题
<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>
使用spring解决hibernate因session关闭导致的延迟加载例外问题。
方法 在web.xml中添加
<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>
现在来看完整的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容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加入spring的filter 用于乱码解决-->
<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>
<!-- 使用spring解决hibernate因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>
<!-- 配置struts的web框架-->
<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>
spring的配置文件 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/test?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
<!-- 配置二级缓存 -->
hibernate.cache.use_second_level_cache=true
<!-- 是否使用查询缓存 -->
hibernate.cache.use_query_cache=true
<!-- 配置使用缓存的驱动类 -->
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启动事务注解管理方式 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
<bean name="/person/list" class="cn.itcast.web.PersonAction"/>
<bean name="/person/pmessage" class="cn.itcast.web.PersonMessageAction"/>
</beans>
缓存配置文件 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>
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>
<form-beans>
<form-bean name="PersonForm" type="cn.itcast.formbean.PersonForm"/>
</form-beans >
<global-forwards>
<forward name="message" path="/WEB-INF/page/message.jsp"></forward>
</global-forwards>
<action-mappings>
<action path="/person/list" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
<action path="/person/pmessage" validate="false" parameter="method"
scope="request" name="PersonForm"></action>
</action-mappings>
<!-- 配置spring的控制器 -->
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
</struts-config>
hibernate的实体beans.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-only" region="cn.itcast.bean.Person"/>
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="10" not-null="true"/>
</class>
</hibernate-mapping>