欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring2.5整合struts2.0+JPA

程序员文章站 2022-04-19 21:33:52
...

整合的步骤如下:(这里强调一点,网上有说用spring去管理struts的Action,这种方式也可以。我已经试过那种方式了,但是我在这里介绍另外一种:在spring的配置文件里面可以不用去配置某个action的service,即在StudentAction.java中可以引用所有的service,根本就不需用配置哪个action来对应哪个service,这种方式比较灵活)

        ×××××××××在附件中有我做的这个项目的示意图。××××××××××

 

1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
	<display-name>struts2.0spring2.5</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<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>

	<filter>
		<filter-name>struts-cleanup</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts-cleanup</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!--Spring Context Config loader-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/conf/applicationContext.xml,classpath:applicationContext-*.xml</param-value>
	</context-param>

	<!-- spirng解决乱码 -->
	<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>

</web-app>

 

 

2. applicationContext.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: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.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<!-- PROPERTIES DEFINITIONS -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>/WEB-INF/conf/jdbc.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- DATASOURCE DEFINITION -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="url" value="${jdbc.url}" />
	</bean>
	
	<!-- ENTITY MANAGER FACTORY -->
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
		<property name="persistenceUnitName" value="Test_Persistence"/>
        <property name="dataSource" ref="dataSource" />
        <property name="loadTimeWeaver">
      		<bean class="com.ctit.tools.MyLoadTimeWeaver"/>
    	</property>
    	<property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
                <property name="showSql" value="false" />
                <property name="generateDdl" value="false" />
            </bean>
        </property>
	</bean>
	
	<!-- TRANSACTION MANAGER -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- ASPECT CONFIGURATION -->
	<aop:config>
		<aop:pointcut id="monsterBigManager" expression="execution(* com.ctit.service.*.*(..))" />
		<aop:advisor pointcut-ref="monsterBigManager" advice-ref="monsterBigAdvice"/>
	</aop:config>	
	 
	<tx:advice id="monsterBigAdvice" transaction-manager="transactionManager">  
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="register*" propagation="REQUIRED"/>
			<tx:method name="create*" propagation="REQUIRED"/> 
			<tx:method name="*" />
		</tx:attributes>	
	</tx:advice>
	
	

	<bean id="studentDao" class="com.ctit.dao.impl.StudentDaoImpl">
		<property name="entityManagerFactory" ref="entityManagerFactory"></property>
	</bean>
	
	<bean id="studentService" class="com.ctit.service.impl.StudentServiceImpl">
		<property name="repository" ref="studentDao"></property>
	</bean>
	         
</beans>           

 

3. struts.xml 一定要在src的目录下或者和src平齐的目录下,我的是在conf这个目录下,conf和src是平齐的目录。
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="action" extends="struts-default">

		<action name="action_*" class="com.ctit.action.StudentAction" method="{1}">
			<result name="list">/studentlist.jsp</result>
			<result name="delete">/studentlist.jsp</result>
			<result name="success">/success.jsp</result>
			<result name="failed">/fail.jsp</result>
			<result name="insert">/studentedit.jsp</result>
		</action>
		
		<!-- App Action-->
		<action name="App" class="com.ctit.action.AppAction">
			<result name="appList">/app/apps.jsp</result>
			<result name="newApp">/app/app-edit.jsp</result>
			<result name="createApp">/app/apps.jsp</result>
			<result name="deleteApp">/app/apps.jsp</result>
			<result name="editApp">/app/app-edit.jsp</result>
			<result name="updateApp">/app/apps.jsp</result>
		</action>
	</package>
</struts>

 

上面是它们整合时的总的最主要的配置文件信息,如果那些都按照我的方法配置基本上不会出现问题的。

下面是附属的配置文件信息:

4. persistence.xml 。位置在src目录下建立一个META-INF目录,它下面有persistence.xml和orm.xml配置信息. 这里的Test_Persistence是和applicationContext.xml里面的persistenceUnitName对应的哦。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
	<persistence-unit name="Test_Persistence" transaction-type="RESOURCE_LOCAL">
		
		<provider>oracle.toplink.essentials.PersistenceProvider</provider>
		
		<mapping-file>META-INF/orm.xml</mapping-file>
		<class>com.ctit.pojo.Student</class>
		
	</persistence-unit>
</persistence>

 

 

5. orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
	version="1.0">
</entity-mappings>

 

 

6. jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
jdbc.url=jdbc\:mysql\://192.168.1.108\:3306/test

 

 

另外还需要说明一点是在applicationContext.xml中还有个配置需要注意:

<property name="loadTimeWeaver">
        <bean class="com.ctit.tools.MyLoadTimeWeaver"/>
</property>

这里的MyLoadTimeWeaver类在下面将会涉及到。

以上是所有的配置文件信息了。

×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

 

 

下面说一说类:

action层,service层都可以不用多说的。 在dao层要说明一点的是:

1. StudentDaoImpl.java, 这里一定要先merge下,然后在remove它。
public class StudentDaoImpl  extends JpaDaoSupport implements IStudentDao {
   public void deleteStudent(Student student) {
       Student mergeStudent = this.getJpaTemplate().merge(student);
        getJpaTemplate().remove(mergeStudent);
   }
}

 

2. 另外也说明另一点,在pojo包中的Student.java,它是普通的javabean,并没有继承什么类的。就像平常那样写就可以了。

 

 

3. 在页面中要用<%@ taglib uri="/struts-tags" prefix="s"%>  
这里必须在WEB-INF下面有struts-tags.tld这个文件的哦。
我这里的项目是在WEB-INF下面有个tags目录,它下面就是了。

 

4. MyLoadTimeWeaver.java
package com.ctit.tools;

import org.springframework.instrument.classloading.SimpleLoadTimeWeaver;

public class MyLoadTimeWeaver extends SimpleLoadTimeWeaver {
	@Override
	public ClassLoader getInstrumentableClassLoader() {
		
		return super.getInstrumentableClassLoader().getParent();
	}
}

 

 

 

至此,上面的也都说完了。保证你可以运行起来的。

×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

 

 下面说说包的作用和问题:

1. 在pojo中的Student.java中用的标注:
@Entity
@Table(name="student")
这些都靠的是:toplink-essentials.jar 这个包在spring的spring-framework-2.5.6\lib\toplink这个目录下有。

 

2. 在applicationContext.xml配置文件中有aop:config这个配置信息。
它靠的是aspectjweaver.jar,这个包在spring-framework-2.5.6\lib\aspectj这个目录下。

 

3. struts2-spring-plugin-2.0.9.jar 这个包一定要加,因为这是整合struts和spring的必须的包。

 

 如果还有什么必须介绍的包我以后在介绍。

 

4.struts2.0的包
struts2-core-2.0.9.jar  xwork-2.0.4.jar(Webwork的核心技术包)   commons-logging-1.0.4.jar
freemarker-2.3.8.jar   ognl-2.6.11.jar