Spring 调用jndi 数据源 struts + hibernate + spring 整合

<?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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    
    <package name="usb" namespace="/add" extends="struts-default">
       <action name="StuHell_*" class="stuHello" method="{1}">
       		<result name="success">/u.jsp</result>
       </action>
    </package>

    <!--package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package-->

    <!-- Add packages here -->

</struts>

 @author liuqing

@version 1.1

@datetime 2010-10-2

jndi 原理这里就不做介绍了

这里是调用tomcat jndi

首先配置 tomcat jndi

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application -->
<Context>
 <Resource name="jdbc/Hellooracle" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@192.168.0.xxx:1521:ORCL"
              username="xxx" password="xxx" maxActive="100" maxIdle="10"
              maxWait="-1"/> 
              
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
	
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

 

配置web.xml 文件 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>hibernate3.6test</display-name>
  
  <resource-ref>
	 <description>Oracle Datasource example</description>
	 <res-ref-name>jdbc/Hellooracle</res-ref-name>
	 <res-type>javax.sql.DataSource</res-type>
	 <res-auth>Container</res-auth>
 </resource-ref>
<!--
	  - Location of the XML file that defines the root application context
	  - Applied by ContextLoaderListener.
	  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext-*.xml
		</param-value>
	</context-param>
		<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<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>
    
    <listener>
       <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

   配置application-app.xml 文件

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans> 

     <bean id="dataSource"
        class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
           <value>java:comp/env/jdbc/Hellooracle</value>
       </property>
    </bean>
    <!--bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">   
        <property name="driverClass" value="oracle.jdbc.OracleDriver" />   
        <property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.0.108:1521:ORCL" />   
        <property name="user" value="bbsqyl" />   
        <property name="password" value="bbsqyl" />   
    </bean-->   
  
    <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
        <property name="dataSource">   
            <ref local="dataSource" />   
        </property>   
        <property name="configLocation">   
            <value>   
                classpath:hibernate.cfg.xml   
            </value>   
        </property>   
        <property name="hibernateProperties">   
            <props>   
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   
                <prop key="hibernate.show_sql">true</prop>   
            </props>   
        </property>   
    </bean>   
  
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >   
        <property name="sessionFactory" ref="sessionFactory" />   
    </bean>   
    <!-- 配置事务拦截器 -->   
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">   
     <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->   
        <property name="transactionManager" ref="transactionManager" />   
        <property name="transactionAttributes">   
          <!-- 下面定义事务传播属性-->   
            <props>   
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>   
                <prop key="*">PROPAGATION_REQUIRED</prop>   
            </props>   
        </property>   
    </bean>   
  
    <!-- 定义BeanNameAutoProxyCreator-->   
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
     <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->   
        <property name="beanNames">   
            <!-- 下面是所有需要自动创建事务代理的bean-->   
            <list>   
                <value>*Service</value>   
            </list>   
            <!-- 此处可增加其他需要自动创建事务代理的bean-->   
        </property>   
        <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->   
        <property name="interceptorNames">   
            <list>   
                <!-- 此处可增加其他新的Interceptor ,下面的拦截器仅用于生成 事务代理-->   
                <value>transactionInterceptor</value>   
            </list>   
        </property>   
    </bean>   
</beans>

 

  添加action 文件

 

 

<?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.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

     <bean id="test" class="com.test.dao.TestSpringConnection">
       <property name="sessionFactory" ref="sessionFactory" />
     </bean>

     <bean id="stuHello" class="com.test.action.StuHello" scope="session" >
     	<property name="usb" value="struts2 successful" />
     	<property name="test" ref="test"></property>
     </bean>
     
</beans>

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值