applicationContext.xml
<bean id="entiryManagerFactory" class=""org.springframeword.orm.jpg.LocalEntityManagerFactoryBean>
<!--这个persistenceUnitName的value等于上面persistence.xml中的persistence-unit的name-->
<property name="persistenceUnitName" value="persistenceUnit"></property>
</bean>
<!-- 使用注解spring事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--服务层实现类-->
<bean name="userService" class="cn.complay.servivce.UserServiceImpl"></bean>
<!--name就是等于struts的映射url-, scope="prototype"就是为了解决struts1线程安全->
<bean name="/user/add" class="cn.complay.action.UserAction" scope="prototype"></bean>
<!--在UserServiceImpl类中得到EntityManager-->
@PersisterceContext private EntityManager entityManager;
//可以调用entityManager.persist(obj)方法了
struts.xml
<struts-config>
<action path="/user/add" validata="false">
<forward name="list" path="/WEB-INF/page/list.jsp"></forward>
</action>
<controller>
<set-perperty property="processorClass" class="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>
</struts-config>
web.xml
<!--实例化spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context/spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--spring 编码格式-->
<filter>
<filter-name>springCharsetEncodingFilter</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>
<!--spring解决jpa中的EntiryManage 关闭导致的延迟加载问题,
请求数据来是session打开,使用数据后session关闭-->
<filter>
<filter-name>entityManagerInView</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntiryManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>entityManagerInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
UserAction.java
//注入 spring管理
@Resource private UserService userService;
就可以调用 userService的方法了
spring 和struts1与jpa集成
最新推荐文章于 2024-08-18 21:36:31 发布