1.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> <!-- 配置struts2中的Action对象交给spring创建 --> <constant name="struts.objectFactory" value="spring"/> <!-- 配置本属性,可以使得改动struts配置文件不用重启应用服务器 --> <constant name="struts.configuration.xml.reload" value="true"/> <package name="crm" namespace="/" extends="struts-default"> <global-results> <result name="pub_add_success"> /common/pub_add_success.jsp </result> <result name="pub_del_success"> /common/pub_del_success.jsp </result> </global-results> <!-- 使用路径模式匹配的方法,减轻配置工作量 --> <action name="*Customer" class="{1}CustomerAction" > <result name="index">/struts2/customer/index.jsp</result> <result name="input">/struts2/customer/{1}_input.jsp</result> <result name="import_input">/struts2/customer/import_input.jsp</result> <result name="import_result">/struts2/customer/import_result.jsp</result> </action> </package> </struts> 2.applicationContext-common.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"> <!-- 建立数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 数据库驱动,我这里使用的是Mysql数据库 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <!-- 数据库地址,这里也要注意一下编码,不然乱码可是很郁闷的哦! --> <property name="url" value="jdbc:mysql://localhost:3306/s2sh01"> </property> <!-- 数据库的用户名 --> <property name="username" value="root"></property> <!-- 数据库的密码 --> <property name="password" value="root"></property> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 把数据源注入给Session工厂 --> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.cache.use_query_cache">true</prop> --> </props> </property> <!-- 配置映射文件 --> <property name="mappingResources"> <list> <value>com/fct/s2sh/model/User.hbm.xml </value> </list> </property> </bean> <!-- 把Session工厂注入给hibernateTemplate --> <!-- 解释一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在执行时自动建立 HibernateCallback 对象,例如:load()、get()、save、delete()等方法。 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.fct.s2sh.service.*.*(..))" /> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" /> </aop:config> </beans> 3.applicationContext-actions.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"> <!-- --> <bean id="userAction" class="com.fct.s2sh.web.struts2.UserAction" scope="prototype"> <property name="userManager" ref="userManager" /> </bean> <bean id="userManager" class="com.fct.s2sh.service.impl.UserManagerImpl"> <property name="userDAO" ref="userDAO"> </property> <!-- <property name="sessionFactory" ref="sessionFactory"></property> --> </bean> <bean id="userDAO" class="com.fct.s2sh.dao.impl.UserDAOImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans> 4.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Spring 的ApplicationContext配置文件的路径,可使用通配符,applicationContext*.xml表示所有以applicationContext开头的xml文件。 context-param是在容器启动后最先被执行的,并且被放入到容器上下文中。在这里引入spring的配置文件, 是供Spring的ContextLoaderListener监听器使用。而这个监听器中会有一个ContextLoade类用来获取这个配置文件中的信息。 从而进行Spring容器的初始化工作。 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <!-- 这个监听器就是为了读取Spring的配置文件,这在上面已经讲到了。 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 这是Spring提供的一个用来防止内存泄漏的监听器。在我们使用struts2框架,或其它的某些类库时,因为它们自身的设计, 会用到Introspector(内省)机制来获取Bean对象的信息。但不幸的是,这些框架或类库在分析完一个类之后却没有将它从内存中清除掉, 内存中还保留有大量的静态资源,而这些东西又无法进行垃圾回收,因此产生了很严重的内存泄漏问题。直接表现为服务器的内存使用会随着时间而不断上升, 最后的结果当然就是服务器当掉。所以在这里加入此监听器,能够帮助我们更好的处理内存资源回收的问题。 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 这是Spring的编码过滤器,我们可以直接拿来用,相信这段配置应该很好理解,不过请大家注意forceEncoding这个参数, 把它设置为true表示不管请求中的编码是什么格式,都将强制采用encoding中设置的编码方式。另外对于响应也将按照encoding指定的编码进行设置。 另外不建议将编码设置成gb2312或是gbk格式,请采用基于Unicode的UTF-8编码 --> <filter> <filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 这个过滤器是个好东西,有了它,我们在使用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> <!-- 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> <!-- 如果不想出现莫名其妙的错误,请控制好这些过滤器映射的顺序。 --> </web-app>