struts1+spring+hibernate整合

    技术2022-06-22  32

    steps:struts or hibernate is first is ok

    1:struts or hibernate and test it

    2:add spring

     

    Be careful: jar conflict . Don't use commons-dbcp.jar in struts, but spring's. or it will occur Exception.

     

    Details:

    1.add spring container: config in web.xml(context-param) or struts(plug-in)

     <!-- 加载spring容器(context-param) -->

      <context-param>

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

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

      </context-param>

      <!-- 监听容器(context)创建 -->

      <listener>

          <listener-class>

              org.springframework.web.context.ContextLoaderListener

          </listener-class>

      </listener>

    2.config action

    careful:<bean name="/add" class="***">,only here use "name" but not "id" on the attribute of bean

     

    web.xml configer

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>userAdd.jsp</welcome-file> </welcome-file-list> <!-- 加载spring容器(context-param) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/applicationContext-*.xml</param-value> </context-param> <!-- 监听容器(context)创建 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>  

     

    struts.cfg.xml

    <struts-config> <data-sources /> <form-beans > <form-bean name="StuForm" type="com.gem.jdbc.forms.StuForm"></form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/addAction" name="StuForm" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="succ" path="/success.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.gem.struts.ApplicationResources" /> </struts-config> 

     

    applicationcontext-dao.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 文件的配置。 --> <bean id="jdbcproperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 此处注意,如果整个文件都点击调转不了,如下面name写错,整个xml文件都不能点击跳转 --> <property name="locations"> <list> <value>WEB-INF/classes/Jdbc.properties</value> </list> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 仅有下面一处用到,采用外置配合文件,datasource不能省略,sessionfactory和transaction都需要注入, 我还没有办法从hibernate。cfg.xml中拿到(尝试失败) --> <property name="configLocations"> <list> <value>WEB-INF/classes/hibernate.cfg.xml</value> </list> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="stuDao" class="com.gem.jdbc.dao.impl.StuDaoImpl"> <property name="ht" ref="hibernateTemplate"></property> </bean> <!-- add HibernateTransaction --> <bean id="hibernateTransaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- below,the difference between jdbcTransactionManager and HibernateTransactionManager --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="hibernateTransaction" /> </beans>  

    applicationcontext-service.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"> <bean name="/addAction" class="com.gem.jdbc.actions.StuAction"> <property name="sbi" ref="stuBizImpl"></property> </bean> <bean id="stuBizImpl" class="com.gem.jdbc.biz.impl.StuBizImpl"> <property name="sd" ref="stuDao"></property> </bean> </beans>  

     


    最新回复(0)