我项目包目录如下:
一.准备包
有些包可以不需要,这里就不详细筛查了
二.在web.xml 里面整合spring 和 strunt2 ,web.xml 内容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>test_1</display-name>
<!-- 加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/*.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!--
ContextLoaderListener的作用就是启动Web容器时,
自动装配ApplicationContext的配置信息。
因为它实现了ServletContextListener这个接口,
在web.xml配置这个监听器,启动容器时,
就会默认执行它实现的方法。
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置页面请求字符为UTF-8 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2 过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
在Java Web项目中使用Hibernate经常会遇到LazyInitializationException。
这是因为controller和model层(java代码)将通过JPA的一些启用了延迟加载功能的领域
(如用getRefrence()方法或者在关联关系中采用fetch=FetchType.LAZY)
返回给view层(jsp代码)的时候,由于加载领域对象的JPA Session已经关闭,
导致这些延迟加载的数据访问异常。
这时就可以使用OpenEntityManagerInViewFilter
来将一个JPAsession与一次完整的请求过程对应的线程相绑定。
-->
<filter>
<filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
三.spring 配置文件如下
spring_applicationContext.xml 配置文件保护连接池,事务,和jpa的配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is the Spring configuration file. This file declares all of the Spring beans that
will be used by the Struts 2 Portfolio, starting with Chapter Nine.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置jpa -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<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="persistenceUnitName" value="persistenceUnit" />
</bean>
<!-- 引入数据库连接属性文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
<value>classpath:log4j.properties</value>
</list>
</property>
</bean>
<!-- 配置数据库连接-->
<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="alias" value="${jdbc-0.proxool.alias}" />
<property name="driver" value="${jdbc-0.proxool.driver-class}" />
<property name="driverUrl" value="${jdbc-0.proxool.driver-url}"/>
<property name="user" value="${jdbc-0.user}" />
<property name="password" value="${jdbc-0.password}" />
<property name="maximumConnectionCount" value="${jdbc-0.proxool.maximum-connection-count}" />
<property name="minimumConnectionCount" value="${jdbc-0.proxool.minimum-connection-count}" />
<property name="simultaneousBuildThrottle" value="${jdbc-0.proxool.simultaneous-build-throttle}" />
</bean>
<!-- 配置事务管理器:这里采用jpa事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory" />
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!--
配置事务属性:事务传播范围、事务隔离级别。
-->
<bean name="transactionAttributesSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="buy*">PROPAGATION_REQUIRED</prop>
<prop key="submit*">PROPAGATION_REQUIRED</prop>
<prop key="goto*">PROPAGATION_REQUIRED</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="do*">PROPAGATION_REQUIRED</prop>
<prop key="execute*">PROPAGATION_REQUIRED</prop>
<prop key="prepairPay">PROPAGATION_REQUIRES_NEW
</prop>
<prop key="debug">PROPAGATION_REQUIRES_NEW
</prop>
<prop key="info">PROPAGATION_REQUIRES_NEW
</prop>
<prop key="error">PROPAGATION_REQUIRES_NEW
</prop>
<prop key="fatal">PROPAGATION_REQUIRES_NEW
</prop>
<prop key="warn">PROPAGATION_REQUIRES_NEW
</prop>
<prop key="assign*">PROPAGATION_REQUIRED
</prop>
<prop key="finish*">PROPAGATION_REQUIRED
</prop>
<prop key="close*">PROPAGATION_REQUIRED
</prop>
<prop key="insert*">PROPAGATION_REQUIRED
</prop>
<prop key="*">PROPAGATION_REQUIRED
</prop>
</props>
</property>
</bean>
<!-- 配置事务拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"> <!-- 事务管理 -->
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource"> <!-- 事务属性 -->
<ref bean="transactionAttributesSource" />
</property>
</bean>
<!-- 自动创建事务代理
使用的是spring默认的jdk动态代理BeanNameAutoProxyCreator。
-->
<bean id="autoTxProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!--
interceptorNames定义事务属性和事务管理器。
-->
<property name="interceptorNames">
<list>
<idref bean="transactionInterceptor" />
</list>
</property>
<!--
拦截范围 beanNames
如下拦截范围是*Service,*Provider,*Connector
表示IOC容器中以Service结尾的bean,
一般配置在spring.xml,serviceContext.xml之类的spring配置文件,
如 <bean id="menuService" class="com.service.impl.MenuServiceImpl"/>
要注意这里不是值src下边的类。
注意:基于jdk动态代理的AOP事务控制,只能针对接口。
-->
<property name="beanNames">
<value>*Service,*Provider,*Connector
</value>
</property>
</bean>
<!-- 配置jdbc模板 -->
<bean id="simpleJdbcDaoSupport"
class="org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
spring_serviceContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="baseDao" class="com.zx.action.dao.BaseDaoImpl"></bean>
<bean id="indexService" class="com.zx.action.service.IndexServiceImpl">
<property name="baseDao" ref="baseDao" />
</bean>
</beans>
下面是 jpa 的配置信息persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> -->
<!--<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />-->
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none"/>
</properties>
</persistence-unit>
</persistence>
上面就是主要的配置文件
完整代码 下载 ,本例子使用的数据库是 sqlserver ,下载后更改下连接字符串,用自己的数据库即可