用maven搭建struts2+spring+hibernate

openkk 14年前發布 | 4K 次閱讀 JProfiler Win7 報表工具

<DIV class=postText>
今天抽空練習下用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&nbsp;
其中有標志的地方可以根據自己需要修改,這樣一個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=";
 xmlns:xsi="
;
 xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee ;
 <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文件
</SPAN>

<?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://www.blogjava.net/Files/lcs868/s2shdemo.zip
</DIV>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!