eclipse搭建SSH框架詳解 .

SSH框架是最常用的框架之一,在搭建SSH框架的時候總有人遇到這樣,那樣的問題。下面我介紹一下SSH框架搭建的全過程。
第一步:準備工作。
  下載好eclipse,Struts2,Spring,Hibernate。
  1.eclipse:eclipse下載的時候建議下載JavaEE版的eclipse。
                 當然你也可以下載eclipse-SDK。(下載eclipse-SDK需要下載Web,Tomcat等plugins)
  2.Struts2:http://struts.apache.org/download 
         1)引入Struts的jar包。下載 struts-*-all.zip 解壓後,struts/lib目錄下是struts所有的相關jar包。 
         其中有5個是必須的:

               Commons-logging-1.0.4.jar,Freemarker-2.3.13.jar, 
               Ognl-2.6.11.jar,Struts2-core-2.1.6.jar,Xwork-2.1.2.jar 
         其餘jar包並不是struts必須的。還有3個包也要注意導入。不導入運行Tomcat時候可能會出現異常。 
               commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar,javassist-3.7.ga.jar 
         注意:javassist-3.7.ga.jar包是在struts2-blank-2.2.1.war示例工程中的web-inf/lib下的。 


  3.Spring:http://www.springsource.com/download/community 
        還可以在eclipse下安裝下載。具體步驟是這樣的:
        1)打開eclipse-help-Software Updates.

 
        2) 在打開的對話框中選擇上面的第二項(Available Software)。

 
        3)點擊Add Site按鈕,彈出URL對話框。 


        4)在對話框裏輸入:http://springide.org/updatesite/點擊OK。 


        5)選擇sping IDE點擊安裝(Install)。


  4.Hibernate:http://sourceforge.net/projects/hibernate/files/hibernate3/

  5.Jdk的src.zip包導入。(當然不導入也可以。。。)

第二步:

  1.創建一個 Web Progect,自己起一個喜歡的名字。

  2.修改WEB-INF下的web.xml文件,增加struts2的配置。

Xml代碼 複製代碼 收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.     <display-name>SSHTest</display-name>  
  7.     <!-- struts Framework -->  
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <filter-class>  
  11.           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter   
  12.         </filter-class>  
  13.     </filter>  
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.     <!-- welcome file -->  
  19.     <welcome-file-list>  
  20.         <welcome-file>index.jsp</welcome-file>  
  21.     </welcome-file-list>  
  22. </web-app>  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.     <display-name>SSHTest</display-name>  
  7.     <!-- struts Framework -->  
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <filter-class>  
  11.           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  12.         </filter-class>  
  13.     </filter>  
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.     <!-- welcome file -->  
  19.     <welcome-file-list>  
  20.         <welcome-file>index.jsp</welcome-file>  
  21.     </welcome-file-list>  
  22. </web-app>  


  3.在WEB-INF/classes目錄下添加struts.xml配置文件: 

Xml代碼 複製代碼 收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC      
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"      
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">     
  5. <struts>  
  6.     <package namespace="/" name="struts2" extends="struts-default">  
  7.         <action name="login" method="execute" class="loginAction">  
  8.             <result name="success">/WEB-INF/jsp/login.jsp</result>  
  9.             <result name="input">/WEB-INF/index.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts>    
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC     
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">    
  5. <struts>  
  6.     <package namespace="/" name="struts2" extends="struts-default">  
  7.         <action name="login" method="execute" class="loginAction">  
  8.             <result name="success">/WEB-INF/jsp/login.jsp</result>  
  9.             <result name="input">/WEB-INF/index.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts>    

 
  4.配置Spring

    1)導入spring包。spring-framework-**.zip解壓後,將spring-framework-**文件夾的dist目錄下的jar包導入工程中。 


    2)配置web.xml文件。 

Xml代碼 複製代碼 收藏代碼
  1. <!-- Spring Framework -->  
  2. <listener>  
  3.     <listener-class>  
  4.       org.springframework.web.context.ContextLoaderListener   
  5.     </listener-class>  
  6. </listener>  
  7. <context-param>  
  8.     <param-name>contextConfigLocation</param-name>  
  9.     <param-value>  
  10.         classpath:/applicationContext*.xml   
  11.     </param-value>  
  12. </context-param>  
  1. <!-- Spring Framework -->  
  2. <listener>  
  3.     <listener-class>  
  4.       org.springframework.web.context.ContextLoaderListener  
  5.     </listener-class>  
  6. </listener>  
  7. <context-param>  
  8.     <param-name>contextConfigLocation</param-name>  
  9.     <param-value>  
  10.         classpath:/applicationContext*.xml  
  11.     </param-value>  
  12. </context-param>  

 
    3)添加applicationContext.xml文件。 

Xml代碼 複製代碼 收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="   
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  9.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.     <!-- Action -->  
  11.     <bean id="loginAction" scope="prototype" class="action.LoginAction"></bean>  
  12. </beans>  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  9.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.     <!-- Action -->  
  11.     <bean id="loginAction" scope="prototype" class="action.LoginAction"></bean>  
  12. </beans>  

 
    4)整合Spring與Struts。在Struts的lib目錄中找到struts2-spring-plugin-*.jar,引入到工程中。


  5.配置Hibernate

        1)解壓縮hibernate-distribution-*.zip。導入hibernate-distribution-*GA/lib/required目錄中的jar包。

                hibernate3.jar                         核心類庫
                antlr-2.7.6.jar                          代碼掃描器,用來翻譯HQL語句
                commons-collections-3.1.jar    Apache Commons包中的一個,包含了一些Apache開發的集合類,

                                                                功能比java.util.*強大
                dom4j-1.6.1.jar                        一個Java的XML API,類似於jdom,用來讀寫XML文件的
                javassist-3.4.GA.jar                 Javassist 字節碼解釋器
                jta-1.1.jar                                標準的JTA API。
                slf4j-api-1.5.2.jar
                slf4j-nop-1.5.2.jar


        2)創建Hibernate配置文件。在WEB-INF/calsses目錄下建立鏈接數據庫的配置文件hibernate.cfg.xml。 
            (本人比較懶,公司電腦中只有Access,也懶得下載別的DBMS。所以例子是連接Access的大家將就看吧。 
            *注意:需要導入Access_JDBC30.jar。 
   hibernate.cfg.xml:

Xml代碼 複製代碼 收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC      
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"      
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="connection.driver_class">  
  8.             com.hxtt.sql.access.AccessDriver      
  9.         </property>  
  10.         <property name="connection.url">  
  11.             jdbc:access:///D:/workspace/SSHTest/TestDatabase.accdb   
  12.         </property>  
  13.         <!--  數據庫連接設置 -->  
  14.         <property name="eclipse.connection.profile">access</property>  
  15.         <property name="connection.username"></property>  
  16.         <property name="connection.password"></property>  
  17.         <property name="dialect">com.hxtt.support.hibernate.HxttAccessDialect</property>  
  18.         <!-- show_sql 生成SQL語句 -->  
  19.         <property name="show_sql">true</property>  
  20.         <!-- SQL dialect 方言 -->  
  21.         <property name="hibernate.dialect">  
  22.             com.hxtt.support.hibernate.HxttAccessDialect      
  23.         </property>  
  24.         <!-- 添加實體類的映射文件-->  
  25.         <mapping resource="Login.hbm.xml" />  
  26.              
  27.         <!-- Annotation方式配置   
  28.         <mapping class="entity.Login"/>  
  29.          -->  
  30.     </session-factory>  
  31. </hibernate-configuration>  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC     
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="connection.driver_class">  
  8.             com.hxtt.sql.access.AccessDriver     
  9.         </property>  
  10.         <property name="connection.url">  
  11.             jdbc:access:///D:/workspace/SSHTest/TestDatabase.accdb  
  12.         </property>  
  13.         <!--  數據庫連接設置 -->  
  14.         <property name="eclipse.connection.profile">access</property>  
  15.         <property name="connection.username"></property>  
  16.         <property name="connection.password"></property>  
  17.         <property name="dialect">com.hxtt.support.hibernate.HxttAccessDialect</property>  
  18.         <!-- show_sql 生成SQL語句 -->  
  19.         <property name="show_sql">true</property>  
  20.         <!-- SQL dialect 方言 -->  
  21.         <property name="hibernate.dialect">  
  22.             com.hxtt.support.hibernate.HxttAccessDialect     
  23.         </property>  
  24.         <!-- 添加實體類的映射文件-->  
  25.         <mapping resource="Login.hbm.xml" />  
  26.             
  27.         <!-- Annotation方式配置  
  28.         <mapping class="entity.Login"/>  
  29.          -->  
  30.     </session-factory>  
  31. </hibernate-configuration>  

 
             注意:單獨使用Hibernate需要創建Session工廠類HibernateSessionFactory.java 
                     (如果用Spring整合就不需要了。Spring會在applicationContext.xml中創建。) 
                      Hibernat 對數據庫的操作是通過Session來實現的,這裏的session不同於頁面間傳遞參數的session, 
                      而是類似於JDBC中的 Connection。Session是Hibernate運作的中心, 
                      對象的生命週期、事務的管理、數據庫的存取都與session息息相關。 
                      而Session是由HibernateSessionFactory創建的,是線程安全的, 
                      可以讓多個執行線程同時存取HibernateSessionFactory而不會有數據共享的問題, 
                      但不能讓多個線程共享一個Session。

       3)Login.hbm.xml文件

Xml代碼 複製代碼 收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  5. <hibernate-mapping package="包名">  
  6.     <class name="類名" table="表名">  
  7.         <id name="主鍵在java類中的字段名" column="對應表中字段" type="類型 ">  
  8.             <generator class="主鍵生成策略"/>  
  9.         </id>  
  10.     </class>  
  11. </hibernate-mapping>  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  5. <hibernate-mapping package="包名">  
  6.     <class name="類名" table="表名">  
  7.         <id name="主鍵在java類中的字段名" column="對應表中字段" type="類型 ">  
  8.             <generator class="主鍵生成策略"/>  
  9.         </id>  
  10.     </class>  
  11. </hibernate-mapping>  

 

  6.Spring整合Hibernate。Spring對hibernate的Session的創建、提交、關閉的整個生命週期進行管理。 
        1)  配置sessionFactory,讓spring來創建Session。在applicationContext.xml中增加如下代碼:

Xml代碼 複製代碼 收藏代碼
  1. <!-- sessionFactory -->  
  2.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  3.        <property name="configLocation">  
  4.            <value>classpath:/hibernate.cfg.xml</value>  
  5.        </property>  
  6.     </bean>  
  1. <!-- sessionFactory -->  
  2.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  3.        <property name="configLocation">  
  4.            <value>classpath:/hibernate.cfg.xml</value>  
  5.        </property>  
  6.     </bean>  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章