`
andrew.yulong
  • 浏览: 167155 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

struts2.0 hibernate3.0 spring2.0 集成开发(一)

阅读更多

    由于公司原因,最近开始搞起来struts2.0 spring2.0 hibernate3.0,以前项目是struts1.X jdon ibatis.      hibernate和spring以前都接触过,不过那时还在学校,东西在就已经还给老师了。最近开始在网上找各种资料。加上以前的一点基础,算是问题不 大。
    先来说说struts2。0。这个框架给我的第一映象是,无非是一个现实层的框架,应该没什么很重要。可是慢慢深入后发现,struts2.0确实比起 struts1.0要进步不少。而且更加接近web2.0了。很不错。hibernate是我一直都很喜欢的持久层框架,嘿嘿。因为sql一直都不好,嘿 嘿。在就是业务层了。spring可以说是我一直都很深入去研究的一个框架。这回有机会也很好。     
     好了,说了这多废话,我们开始第一节的内容吧.    第一节嘛,首先帮大家配置配置环境啦。让大家有个感性的认识。先说道配置环境,首先谈到这3个框架所需的jar吧,为了避免大家去各个官网去下,我准备把这3个框架所需要的 jar打包传到csdn的,可是大小超过了10M传不上去,大家还是去下吧....
    在来介绍一下这3个框架用的配置文件。

  • applicationContext.xml这是spring的配置的文件(用来配置bean和数据库的配置)
  • struts.xml 和struts1的配置文件差不多,用来负责action的配置
  • struts.properties 这个文件大家应该比较陌生,这是用来配置struts在运行时的一些参数
  • web。xml这不要说吧。

    需要说明的一点就是这里没有hibernate的配置文件,因为已经集成到spring的配置文件里面去了。

  •     这里先从web.xml开始说起吧,他就像是房子的地基。不可或缺,这个文件没什么好说的,大家注意下里面的注释就可以了。
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    >
        
    <display-name>ASK123</display-name>
        
    <!-- 用来加载spring的配置文件 -->
        
    <context-param>
            
    <param-name>contextConfigLocation</param-name>
            
    <param-value>
                /WEB-INF/applicationContext.xml
            
    </param-value>
        
    </context-param>
        
    <context-param>
            
    <param-name>log4jConfigLocation</param-name>
            
    <param-value>/WEB-INF/log4j.properties</param-value>
        
    </context-param>
        
    <listener>
            
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        
    </listener>
        
    <listener>
            
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        
    </listener>
        
    <filter>
            
    <filter-name>encodingFilter</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>
        
    <!-- z这里要特别注意,这里是在加载struts的核心类FilterDispatcher -->
        
    <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>

        
    <welcome-file-list>
            
    <welcome-file>login.jsp</welcome-file>
        
    </welcome-file-list>
    </web-app>
  • 我们在来看看spring的配置文件。这个文件用来处理hibernate的一些数据库配置和管理bean
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        
    <bean id="propertyConfigurer"
            class
    ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            
    <property name="locations">
                
    <list>
                    
    <!-- 这里面我把连接数据库的参数封装到了datasource.properties这个文件里 -->
                    
    <value>classpath:datasource.properties</value>
                
    </list>
            
    </property>
        
    </bean>
        
    <!-- 从datasource.properties这个文件获得参数 -->
        
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
            dependency-check
    ="none">
            
    <property name="driverClass">
                
    <value>${datasource.driverClassName}</value>
            
    </property>
            
    <property name="jdbcUrl">
                
    <value>${datasource.url}</value>
            
    </property>
            
    <property name="user">
                
    <value>${datasource.username}</value>
            
    </property>
            
    <property name="password">
                
    <value>${datasource.password}</value>
            
    </property>
            
    <property name="acquireIncrement">
                
    <value>${c3p0.acquireIncrement}</value>
            
    </property>
            
    <property name="initialPoolSize">
                
    <value>${c3p0.initialPoolSize}</value>
            
    </property>
            
    <property name="minPoolSize">
                
    <value>${c3p0.minPoolSize}</value>
            
    </property>
            
    <property name="maxPoolSize">
                
    <value>${c3p0.maxPoolSize}</value>
            
    </property>
            
    <property name="maxIdleTime">
                
    <value>${c3p0.maxIdleTime}</value>
            
    </property>
            
    <property name="idleConnectionTestPeriod">
                
    <value>${c3p0.idleConnectionTestPeriod}</value>
            
    </property>
            
    <property name="maxStatements">
                
    <value>${c3p0.maxStatements}</value>
            
    </property>
            
    <property name="numHelperThreads">
                
    <value>${c3p0.numHelperThreads}</value>
            
    </property>
        
    </bean>
        
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            
    <property name="dataSource">
                
    <ref local="dataSource" />
            
    </property>
            
    <!-- 加载hibernate的hbm.xml文件这个文件用映射表和model之间的关系 -->
            
    <property name="mappingResources">
                
    <list>
                    
    <value>net/ask123/ecommerce/domain/User.hbm.xml</value>
                
    </list>
            
    </property>
            
    <property name="hibernateProperties">
                
    <props>
                    
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    
    <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
                    
    <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
                
    </props>
            
    </property>
        
    </bean>
        
    <!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 -->
        
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            
    <!-- 为事务管理器注入sessionFactory" -->
            
    <property name="sessionFactory" ref="sessionFactory" />
        
    </bean>
        
    <!-- 配置事务拦截器Bean -->
        
    <bean id="transactionInterceptor"
            class
    ="org.springframework.transaction.interceptor.TransactionInterceptor">
            
    <!-- 为事务拦截器bean注入一个事物管理器 -->
            
    <property name="transactionManager" ref="transactionManager"></property>
            
    <property name="transactionAttributes">
                
    <!-- 定义事务传播属性 -->
                
    <props>
                    
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="update*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="add*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                    
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                    
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                    
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                    
    <prop key="change*">PROPAGATION_REQUIRED</prop>
                    
    <!-- <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> -->
                
    </props>
            
    </property>
        
    </bean>
        
    <!-- 定义BeanNameAutoProxyCreator -->
        
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            
    <!-- 指定满足哪些bean自动生成业务代理 -->
            
    <property name="beanNames">
                
    <!-- 需要自动创建事务代理的bean -->
                
    <list>
                    
    <value>userService</value>
                
    </list>
                
    <!-- 其它需要自动创建事务代理的bean -->
            
    </property>
            
    <property name="interceptorNames">
                
    <list>
                    
    <value>transactionInterceptor</value>
                    
    <!-- 可增加其它的interceptor -->
                
    </list>
            
    </property>
        
    </bean>
    </beans>
  • 往下就是struts。xml了这个文件和以前的struts的配置文件差不多
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd" 
    >
    <struts>
        
    <package name="" namespace="" extends="struts-default">
            
    <!-- 配置一个action -->
            
    <action name="login" class="loginAction">
                
    <!-- 这里的type指的是模板,默认是jsp -->
                
    <!--  这里的意思是登录成功后跳转到 /index.jsp 页面 -->
                
    <result name="success">
                    
    <param name="location">/welcome.jsp</param>
                
    </result>
                
    <!--  登录失败后跳转到 /error.jsp 页面 -->
                
    <result name="error" type="dispatcher">
                    
    <param name="location">/error.jsp</param>
                
    </result>
                
分享到:
评论
2 楼 hongshenghome 2009-03-18  
请问怎样向多个action配置事物啊.spring1.2中可用事物模板,不知道spring2.x呢?
1 楼 hongshenghome 2009-03-18  
不错!支持你!!!

相关推荐

Global site tag (gtag.js) - Google Analytics