<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    用maven搭建struts2+spring+hibernate

    Posted on 2008-12-15 17:50 李春生 閱讀(4124) 評論(1)  編輯  收藏 所屬分類: maven2

    今天抽空練習下用maven2+myeclipse搭建一個ssh框架,把搭建過程簡單記錄一下,以便以后方便查閱
    1.準備工作:jdk1.6,maven2.0.9,myeclipse6.0.1,mysql數據庫

    2.jdk的安裝配置,以及maven2在eclipse里的配置網上的例子很多就不在重復

    3.用m2搭建主項目工程s2shdemo,在myeclipse中點擊new-other-maven project-create a simple project輸入groupId,ArtifactId,在這里別的地方都可以根據自己的愛好填寫,但打包方式packaging必須選擇pom選項。點下一步后點擊完成即可。

    4.完成web層:我在這里用的是m2的命令行搭建一個web工程,進入cmd運行如下命令:
    mvn archetype:create -DgroupId=com.lcsssh.web.action -DartifactId=s2shdemo -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-starter -DarchetypeVersion=2.0.11.2-SNAPSHOT -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository 
    其中有標志的地方可以根據自己需要修改,這樣一個struts2和spring的web工程就搭建成功。
    5.完成service層:在myeclipse中選擇file-other-maven module填寫module name并選擇父工程,也就是第3部你所建立起來的工程。進入下一步就如第三步一樣填寫所需選項,記住這里packaging應選擇jar或其他不能用pom。

    6.如第五步搭建dao層。

    7.整合:以上步驟完成后開始配置,我把配置有關的文件全部放到web模塊下的resources目錄下
        配置web.xml文件,加入如下代碼
    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
     <display-name>s2shdemo</display-name>  
     <distributable />  
     <context-param>  
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:applicationContext.xml</param-value>  
     </context-param>  
     <listener>  
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
     </listener>  
     <filter>  
      <filter-name>struts</filter-name>  
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
      <init-param>  
       <param-name>actionPackages</param-name>  
       <param-value>cn.allwap.backend.action</param-value>  
      </init-param>  
     </filter>  
     <servlet>  
      <servlet-name>dwr</servlet-name>  
      <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>  
      <init-param>  
       <param-name>debug</param-name>  
       <param-value>true</param-value>  
      </init-param>  
     </servlet>    
     <filter>  
      <filter-name>openSession</filter-name>  
      <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
      <init-param>  
       <param-name>singleSession</param-name>  
       <param-value>false</param-value>  
      </init-param>  
     </filter>  
     <filter>  
      <filter-name>struts-cleanup</filter-name>  
      <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>  
     </filter>  
     <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>  
     <filter-mapping>  
      <filter-name>encodingFilter</filter-name>  
      <url-pattern>/*</url-pattern>  
     </filter-mapping>  
     <filter-mapping>  
      <filter-name>openSession</filter-name>  
      <url-pattern>/*</url-pattern>  
     </filter-mapping>  
     <servlet-mapping>  
      <servlet-name>dwr</servlet-name>  
      <url-pattern>/dwr/*</url-pattern>  
     </servlet-mapping>  
     <filter-mapping>  
      <filter-name>struts-cleanup</filter-name>  
      <url-pattern>/*</url-pattern>  
     </filter-mapping>  
     <filter-mapping>  
      <filter-name>struts</filter-name>  
      <url-pattern>/*</url-pattern>  
     </filter-mapping>  
       <!-- Spring 刷新Introspector防止內存泄露 -->  
        <listener>  
            <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
        </listener>  
      
           
        <!-- session超時定義,單位為分鐘 -->  
        <session-config>  
            <session-timeout>10</session-timeout>  
        </session-config>
     <welcome-file-list>  
      <welcome-file>index.jsp</welcome-file>  
      <welcome-file>default.jsp</welcome-file>  
      <welcome-file>index.html</welcome-file>  
     </welcome-file-list>  
    </web-app>  
    配置web action applicationContext-action.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.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

         <bean id="helloWorldAction" class="com.lcsssh.web.action.HelloWorldAction"  scope="prototype"/>
          <bean id="adminInfoAction" class="com.lcsssh.web.action.AdminInfoAction"  scope="prototype">
           <property name="adminInfoServiceImpl" ref="adminInfoServiceImpl"/>
          </bean>
    </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:lang="http://www.springframework.org/schema/lang"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"  
     default-autowire="byName">  
      <bean id="adminInfoServiceImpl" class="com.lcsssh.service.impl.AdminInfoServiceImpl" scope="prototype">
       <property name="adminInfoDAO" ref="adminInfoDAO"></property>
      </bean> 
    </beans>    
    配置dao 層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:lang="http://www.springframework.org/schema/lang"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"  
     default-autowire="byName">    
      <bean id="adminInfoDAO" class="com.lcsssh.dao.impl.AdminInfoDAO">
       <property name="sessionFactory">
        <ref bean="sessionFactory"/>
       </property>
      </bean>
    </beans>
    配置application.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:lang="http://www.springframework.org/schema/lang"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">  
     
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
      <property name="driverClassName">  
       <value>com.mysql.jdbc.Driver</value>  
      </property>  
      <property name="url">  
       <value>jdbc:mysql://localhost/test</value>  
      </property>  
      <property name="username">  
       <value>user</value>  
      </property>  
      <property name="password">  
       <value>123456</value>  
      </property>  
      <property name="maxActive">  
       <value>20</value>  
      </property>  
      <property name="maxIdle">  
       <value>5</value>  
      </property>  
      <property name="maxWait">  
       <value>-1</value>  
      </property>  
      <property name="defaultAutoCommit">  
       <value>true</value>  
      </property>  
     </bean>  
      
        
      
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
      <property name="dataSource">  
       <ref local="dataSource" />  
      </property>     
      <property name="mappingDirectoryLocations">  
       <list>  
        <value>classpath:com/lcsssh/bo</value>  
       </list>  
      </property>     
      <property name="hibernateProperties">  
       <props>  
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
        <prop key="hibernate.show_sql">false</prop>  
        <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>  
        <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>  
        <prop key="hibernate.cache.use_query_cache">true</prop>  
       </props>  
      </property>  
     
     </bean>  
      <!-- dao object defintions -->
      <import resource="applicationContext-dao.xml"></import>
      <!-- business object defintions -->
      <import resource="applicationContext-service.xml"></import>
      <!-- web action object defintions -->
      <import resource="applicationContext-action.xml"></import>  
     <bean id="transactionManager"  
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
      <property name="sessionFactory">  
       <ref bean="sessionFactory" />  
      </property>  
     </bean>  
      
     <bean id="transactionInterceptor"  
      class="org.springframework.transaction.interceptor.TransactionInterceptor">  
      <property name="transactionManager" ref="transactionManager"></property>  
      <property name="transactionAttributes">  
       <props>  
        <prop key="save*">PROPAGATION_REQUIRED</prop>  
        <prop key="add*">PROPAGATION_REQUIRED</prop>  
        <prop key="set*">PROPAGATION_REQUIRED</prop>  
        <prop key="update*">PROPAGATION_REQUIRED</prop>  
        <prop key="delete*">PROPAGATION_REQUIRED</prop>  
        <prop key="register">PROPAGATION_REQUIRED</prop>          
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
        <prop key="valid*">PROPAGATION_REQUIRED,readOnly</prop>  
        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
       </props>  
      </property>  
     </bean>   
     <bean  
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
      <property name="beanNames">  
       <value>*Service</value>  
      </property>  
      <property name="interceptorNames">  
       <value>transactionInterceptor</value>  
      </property>  
     </bean>   
     <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">  
      <property name="transactionInterceptor" ref="transactionInterceptor">  
      </property>  
     </bean>   
    </beans> 
    所有配置文件配置完成。然后在父工程pom.xml下加入第4步創建的web模塊工程,主要代碼如下所示
    <modules>
      <module>webProject</module>
      <module>serviceProject</module>
      <module>daoProject</module>
     </modules>
    好了,到此為止所有的配置完成

    8.根據數據庫表生成dao 層代碼,用myeclipse的生成工具生成即可。另外工程中所有依賴包都在web工程下的pom.xml中
    為了節省篇幅,沒有截圖,希望大家體諒,本人水平有限,有不當之處敬請包含!!!
    代碼下載地址:http://m.tkk7.com/Files/lcs868/s2shdemo.zip

    Feedback

    # re: 用maven搭建struts2+spring+hibernate[未登錄]  回復  更多評論   

    2008-12-17 21:16 by 憶風
    看上去不錯的樣子

    posts - 5, comments - 10, trackbacks - 0, articles - 23

    Copyright © 李春生

    主站蜘蛛池模板: 亚洲AV日韩AV永久无码色欲| aa级毛片毛片免费观看久| 亚洲黄片毛片在线观看| 中文字幕不卡免费高清视频| 亚洲精品免费在线视频| 免费在线观看的黄色网址| 久久久久久AV无码免费网站下载| 亚洲中文字幕无码久久| 国产亚洲无线码一区二区 | 水蜜桃视频在线观看免费播放高清 | 免费国产黄网站在线观看可以下载 | 91精品国产免费久久国语蜜臀| 亚洲人成网站999久久久综合| 亚洲中文字幕久久精品无码喷水 | 亚洲国产成人久久一区久久| 中文字幕天天躁日日躁狠狠躁免费| 亚洲国产一区二区三区在线观看| 亚洲AV无一区二区三区久久| 日韩视频免费一区二区三区| 人妻无码久久一区二区三区免费| 老司机午夜性生免费福利| 亚洲免费在线视频播放| 亚洲精品无码永久在线观看你懂的| 四虎免费在线观看| 3344永久在线观看视频免费首页 | 国产成人无码区免费网站| 老司机午夜在线视频免费观| 亚洲av片不卡无码久久| 亚洲AV无码一区二区乱孑伦AS| 又大又硬又爽免费视频| 好男人www免费高清视频在线| 久热免费在线视频| 国产精品一区二区三区免费| 亚洲a∨国产av综合av下载| 亚洲午夜久久久久久尤物| 亚洲AV永久精品爱情岛论坛| 国产乱辈通伦影片在线播放亚洲 | 亚洲 欧洲 日韩 综合在线| 久久精品国产亚洲AV无码麻豆| 亚洲精品无码成人片久久| 四虎亚洲国产成人久久精品|