Struts2.3.1.2+Spring3.3.1+Hibernate4.1.1_配置步骤教程



Struts2.3.1.2+Spring3.3.1+Hibernate4.1.1_配置步骤.

一. 搭建struts-2.3.1.2

1.Jar包:

Struts2.3.1.2+Spring3.3.1+Hibernate4.1.1_配置步骤

2.配置struts2.3的过滤器,web.xml位置

Struts2.3.1.2+Spring3.3.1+Hibernate4.1.1_配置步骤

3.web.xml

<?xml version=“1.0″ encoding=“UTF-8″?>

<web-app id=“WebApp_ID” version=“3.0″

xmlns=“http://java.sun.com/xml/ns/javaee”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

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_3_0.xsd”>

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

4.配置struts.xml

Struts2.3.1.2+Spring3.3.1+Hibernate4.1.1_配置步骤

<?xml version=“1.0″ encoding=“UTF-8″ ?>

<!DOCTYPE struts PUBLIC

“-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”

“http://struts.apache.org/dtds/struts-2.3.dtd”>

<struts>

<!– 默认的配置包 –>

<package name=“ssh” extends=“struts-default,json-default,jfreechart-default,spring-default”>

<action name=“login” class=“loginAction”>

<result name=“ok”>/loginOk.jsp</result>

<result name=“error”>/loginError.jsp</result>

</action>

</package>

</struts>

5.测试struts

二.整合Spring 3.1.1和Struts-2.3.1.2

1.jar包

在配置好的strutsjar的基础上,添加额外Struts jar包:struts2-spring-plugin-2.3.1.2.jar

commons-logging-1.1.1.jar

Spring的jar包:

org.springframework.core-3.1.1.RELEASE.jar

org.springframework.context-3.1.1.RELEASE.jar

org.springframework.beans-3.1.1.RELEASE.jar

org.springframework.asm-3.1.1.RELEASE.jar

org.springframework.expression-3.1.1.RELEASE.jar

org.springframework.web-3.1.1.RELEASE.jar

2.web.xml

<?xml version=“1.0″ encoding=“UTF-8″?>

web-app id=“WebApp_ID” version=“3.0″

xmlns=“http://java.sun.com/xml/ns/javaee”

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

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_3_0.xsd”>

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

<!– spring创建工厂监听器 –>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!– 告知spring config location 的存储位置 –>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/applicationContext.xml</param-value>

</context-param>

</web-app>

3.配置spring

Struts2.3.1.2+Spring3.3.1+Hibernate4.1.1_配置步骤

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:p=”http://www.springframework.org/schema/p”

xmlns:tx=”http://www.springframework.org/schema/tx” xmlns:context=”http://www.springframework.org/schema/context”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd”>

<context:annotation-config />

<context:component-scan base-package=”com.excel” />

</beans>

三.整合Hibernate4.1.1

1.jar包

添加Hibernate jar文件

Hibernate压缩包中的

lib/required/

antlr-2.7.7.jar

dom4j-1.6.1.jar

hibernate-commons-annotations-4.0.1.Final.jar

hibernate-core-4.1.1.Final.jar

hibernate-jpa-2.0-api-1.0.1.Final.jar

javassist-3.15.0-GA.jar

jboss-logging-3.1.0.GA.jar

jboss-transaction-api_1.1_spec-1.0.0.Final.jar

lib/jpa/

hibernate-entitymanager-4.1.1.Final.jar

lib/optional/c3p0/

hibernate-c3p0-4.1.1.Final.jar

c3p0-0.9.1.jar

Spring中的jar文件

org.springframework.aop-3.1.1.RELEASE.jar

org.springframework.jdbc-3.1.1.RELEASE.jar

org.springframework.orm-3.1.1.RELEASE.jar

org.springframework.transaction-3.1.1.RELEASE.jar


其他的Spring支持文件

aopalliance.jar — AOP要用到的jar包

以及连接db2的jar

Db2jcc.Jar

以及支持el表达式的standard.jar

2.配置文件

<?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:jee=“http://www.springframework.org/schema/jee”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:p=“http://www.springframework.org/schema/p”

xmlns:util=“http://www.springframework.org/schema/util”

xmlns:tool=“http://www.springframework.org/schema/tool”

xmlns:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation=“http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/jee

http://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/tool

http://www.springframework.org/schema/tool/spring-tool.xsd”

default-lazy-init=“true” default-autowire=“byName”>

<!–配置数据源 –>

<bean id=“dataSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource” destroy-method=“close”>

<property name=“driverClass” value=“com.ibm.db2.jcc.DB2Driver” />

<property name=“jdbcUrl” value=“jdbc:db2://192.168.1.35:50000/ZL_DEMO” />

<property name=“user” value=“db2admin” />

<property name=“password” value=“Forwor888″ />

</bean>

<!– hibernate.cfg.xml Spring config –>

<bean id=“sessionFactory” class=“org.springframework.orm.hibernate4.LocalSessionFactoryBean”>

<!– connection –>

<property name=“dataSource”>

<ref local=“dataSource”/>

</property>

<!– hibernate自身属性 –>

<property name=“hibernateProperties”>

<props>

<prop key=“hibernate.show_sql”>true</prop>

<prop key=“hibernate.format_sql”>true</prop>

<prop key=“hibernate.dialect”>org.hibernate.dialect.DB2Dialect</prop>

</props>

</property>

<!– 映射文件 –>

<property name=“mappingResources”>

<list>

<value>com/forwor/ssh/entity/xml/User.hbm.xml</value>

</list>

</property>

</bean>

<!– 注入 –>

<bean id=“userDAO” class=“com.forwor.ssh.dao.impl.UserDAOImpl”>

<property name=“sessionFactory”>

<ref local=“sessionFactory”/>

</property>

</bean>

<!– aop –>

<!– transaction –>

<bean id=“userBiz” class=“com.forwor.ssh.biz.impl.UserBizImpl”>

<property name=“userDAO”>

<ref local=“userDAO”/>

</property>

</bean>

<!– advice –>

<bean id=“transactionManager” class=“org.springframework.orm.hibernate4.HibernateTransactionManager”>

<property name=“sessionFactory”>

<ref local=“sessionFactory”/>

</property>

</bean>

<!– 事务处理 –>

<bean id=“transactionInterceptor” class=“org.springframework.transaction.interceptor.TransactionInterceptor”>

<property name=“transactionManager”>

<ref local=“transactionManager”/>

</property>

<property name=“transactionAttributes”>

<props>

<prop key=“register”>PROPAGATION_REQUIRED</prop>

<!–hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到–>

<prop key=“get*”>PROPAGATION_REQUIRED,readOnly</prop>

<prop key=“find*”>PROPAGATION_REQUIRED,readOnly</prop>

<prop key=“select*”>PROPAGATION_REQUIRED,readOnly</prop>

<prop key=“query*”>PROPAGATION_REQUIRED,readOnly</prop>

<prop key=“sync*”>PROPAGATION_REQUIRED</prop>

<prop key=“finish*”>PROPAGATION_REQUIRED</prop>

<prop key=“add*”>PROPAGATION_REQUIRED</prop>

<prop key=“insert*”>PROPAGATION_REQUIRED</prop>

<prop key=“edit*”>PROPAGATION_REQUIRED</prop>

<prop key=“update*”>PROPAGATION_REQUIRED</prop>

<prop key=“save*”>PROPAGATION_REQUIRED</prop>

<prop key=“remove*”>PROPAGATION_REQUIRED</prop>

<prop key=“delete*”>PROPAGATION_REQUIRED</prop>

<prop key=“*”>PROPAGATION_REQUIRED,-java.lang.Exception</prop>

</props>

</property>

</bean>

<!– autoproxy 自动创建代理–>

<bean id=“ProxyCreator” class=“org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator>

<property name=“beanNames”>

<list>

<value>*Biz</value>

</list>

</property>

<property name=“interceptorNames”>

<list>

<value>transactionInterceptor</value>

</list>

</property>

</bean>

<bean id=“loginAction” class=“com.forwor.ssh.web.LoginAction” scope=“prototype”>

<property name=“userBiz”>

<ref local=“userBiz”/>

</property>

</bean>

</beans>

http://blog.sina.com.cn/s/blog_6798793901016gxa.html