`

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>

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Spring应用开发完全手册 源码

    本书以使用Spring框架技术开发网络应用程序的实用技术为中心,全面、系统地...运用Spring+Hibernate开发校园管理系统,运用Spring+Struts+Hibernate开发企业门户网站,运用Spring+Java Swing开发企业进销存管理系统等。

    Spring in Action(第二版 中文高清版).part2

    5.2.1 使用JNDI数据源 5.2.2 使用数据源连接池 5.2.3 基于JDBC驱动的数据源 5.3 在Spring里使用JDBC 5.3.1 处理失控的JDBC代码 5.3.2 使用JDBC模板 5.3.3 使用Spring对JDBC的DAO支持类 5.4 在Spring里集成...

    Spring in Action(第二版 中文高清版).part1

    5.2.1 使用JNDI数据源 5.2.2 使用数据源连接池 5.2.3 基于JDBC驱动的数据源 5.3 在Spring里使用JDBC 5.3.1 处理失控的JDBC代码 5.3.2 使用JDBC模板 5.3.3 使用Spring对JDBC的DAO支持类 5.4 在Spring里集成...

    Spring in Action(第2版)中文版

    5.2.1使用jndi数据源 5.2.2使用数据源连接池 5.2.3基于jdbc驱动的数据源 5.3在spring里使用jdbc 5.3.1处理失控的jdbc代码 5.3.2使用jdbc模板 5.3.3使用spring对jdbc的dao支持类 5.4在spring里集成hibernate ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    配置子报表数据源 14.7.5. 配置Exporter的参数 15. 集成其它Web框架 15.1. 简介 15.2. 通用配置 15.3. JavaServer Faces 15.3.1. DelegatingVariableResolver 15.3.2. FacesContextUtils 15.4. Struts 15.4.1. ...

    Spring面试题

    用户提交表单时,一个配置好的ActionForm对象被创建,并被填入表单相应的数据,ActionServler根据Struts-config.xml文件配置好的设置决定是否需要表单验证,如果需要就调用ActionForm的 Validate()验证后选择将...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

     本书是《轻量级java ee企业应用实战》的姊妹篇,《轻量级java ee企业应用实战》主要介绍以spring+hibernate为基础的java ee应用;本书则主要介绍以ejb 3+jpa为基础的java ee应用。ejb 3、jpa规范都属于sun公司所...

    J2EE应用开发详解

    325 18.4.2 配置数据库连接池 327 18.5 HQL简介 328 18.6 小结 330 第19章 权限管理系统(Struts+Spring+Hiberante+Ajax) 331 19.1 需求分析 331 19.2 系统总体流程设计 331 19.3 系统设计 332 19.4 系统总体解析 ...

    千方百计笔试题大全

    105、spring工作机制及为什么要用? 24 106、HttpSession session = request.getSession() 24 107、getParameter与 getAttribute的区别? 24 108、以下哪一个不是赋值符号? 25 109、以下哪个不是Collection的子接口?...

    java面试宝典

    105、spring工作机制及为什么要用? 24 106、HttpSession session = request.getSession() 24 107、getParameter与 getAttribute的区别? 24 108、以下哪一个不是赋值符号? 25 109、以下哪个不是Collection的子接口?...

    JAVA上百实例源码以及开源项目源代码

    5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...

    JAVA上百实例源码以及开源项目

    5个目标文件,演示Address EJB的实现,创建一个EJB测试客户端,得到名字上下文,查询jndi名,通过强制转型得到Home接口,getInitialContext()函数返回一个经过初始化的上下文,用client的getHome()函数调用Home接口...

Global site tag (gtag.js) - Google Analytics