s2sja的总结流程
1。创建WEB工程
2。增加对JPA的支持。
3。增加对spring——JPA的支持。注意选中spring-web包
4.删除asm-2.23.jar(解决冲突)
5.加入STRUTS2的JAR包
7.JPA反向工程生成springDAO!.
------------------------------------------------------------------------------------------------------------------------------------------
8.填写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">
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
----------------------------------------------------------------------------------------------------------------------------------
9.填写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>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default">
<action name="list" method="execute" class="UserAction">
<result>pages/list.jsp</result>
<result name="input">pages/list.jsp</result>
</action>
<action name="remove" class="UserAction" method="remove">
<result>pages/list.jsp</result>
<result name="input">pages/list.jsp</result>
</action>
<action name="save" class="UserAction" method="save">
<result>pages/list.jsp</result>
<result name="input">pages/list.jsp</result>
</action>
</package>
</struts>
--------------------------------------------------------------------------------------------------------------------------------------------
10.填写applicationContext.xml(位于web-inf下)注意整合的时候spring的配置文件的位置,jpa和spring的默认位置并不一样,可以通过在WEB.XML里配置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"
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" xmlns:tx="http://www.springframework.org/schema/tx">
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="ssjpPU" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" /><bean
id="UserDAO" class="user.UserDAO">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<bean id="UserAction" scope="prototype"
class="user.UserAction">
</bean>
</beans>
---------------------------------------------------------------------------------------------------------------------
11.使用spring-jpa的dao****************************************************************************************
---------------------------------------------------------------------------------------------------------------------
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"/applicationContext.xml");
JpaTransactionManager txManager = ((JpaTransactionManager) ctx.getBean("transactionManager"));
IUserDAO dao = UserDAO.getFromApplicationContext(ctx);
TransactionStatus status = txManager .getTransaction(new DefaultTransactionDefinition());
User user=new User("清秋11","588629",null);
dao.save(user);
txManager.commit(status);
System.out.println("数据已经保存!");
-----------------------------------------------------------------------------------------------------------------------
12.在页面处使用dojo来显示!****************************************************************************************
-----------------------------------------------------------------------------------------------------------------------