MVC框架之SSH(Spring+Struts+Hibernate)框架學習(一)

 

Struts2
引入struts所需的包:
struts2-core-2.3.4.1.jar  struts2核心包
xwork-core-2.3.4.1.jar xwork核心包
antlr-2.7.2.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
ognl-3.0.5.jar
struts2-spring-plugin-2.3.4.1.jar struts的spring插件
 
配置過濾器:
web.xml配置
 
 
<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 >


Spring
引入spring所需的包:
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.asm-3.1.2.RELEASE.jar
org.springframework.beans-3.1.2.RELEASE.jar
org.springframework.context.support-3.1.2.RELEASE.jar
org.springframework.context-3.1.2.RELEASE.jar
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.expression-3.1.2.RELEASE.jar
org.springframework.jdbc-3.1.2.RELEASE.jar
org.springframework.orm-3.1.2.RELEASE.jar   封裝了hibernate,ibatis等持久層框架
org.springframework.transaction-3.1.2.RELEASE.jar  封裝了DAOSurpport
org.springframework.web.struts-3.1.2.RELEASE.jar  
org.springframework.web-3.1.2.RELEASE.jar  
 
 
web.xml中的配置(監聽器):
<context-param>
   <param-name >contextConfigLocation</ param-name>
   <param-value >/WEB-INF/classes/applicationContext.xml</param-value >
</context-param>
<listener>
     <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
 
 
Hibernate3
 引入所需的包:
hibernate3.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
dom4j-1.6.1.jar
 
SSH整合
1.spring 整合 struts
一、需要的JAR文件爲:Spring和Struts2框架本身需要的JAR文件以及他們所依賴的JAR文件,比如commons-logging.jar等等,另外還需要Struts2發佈包中的struts2-spring-plugin-x.xx.jar。

二、在web.xml中增加WebApplicationContext的相應配置,以下兩種配置方式本質是一樣的。
1.         Servlet 2.3及以上版本可以使用監聽器,相應配置如下:
<context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果spring配置文件被命名爲applicationContext.xml,並且放在WEB-INF目錄下,則不需要配置<context-param>,因爲ContextLoaderListener默認在WEB-INF目錄下尋找名爲applicationContext.xml的文件。若存在多個Spring配置文件,則在<param-value>中依次列出,之間以逗號隔開。
 
 
2.spring整合hibernate
 
applicationContext.xml如下:
 
<?xml version="1.0" encoding= "UTF-8"?>
<!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >

<beans>

    <bean id="sessionfactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        <property name="configLocation" >
            <value> classpath:hibernate.cfg.xml</value >
        </property>
       
        <property name="dataSource" > 
             <ref bean="dataSource" /> 
      </property >
    </bean >
   
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" >
         <value> com.mysql.jdbc.Driver</value >
        </property>
        <property name="url" >
         <value> jdbc:mysql://localhost:3306/test</value >
        </property>
        <property name="username" value="root"></ property>
        <property name="password" value="123456"></ property>
       </bean>

</beans>


hibernate.cfg.xml如下:
<!DOCTYPE hibernate-configuration PUBLIC
         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
 
 <hibernate-configuration >
 
     <session-factory >
 
         <!-- Database connection settings -->
        <!--          <property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
<!--          <property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property> -->
<!--          <property name="connection.username">root</property> -->
<!--          <property name="connection.password">123456</property> -->
 
         <!-- SQL dialect -->
         <property name="dialect" >org.hibernate.dialect.MySQLDialect</ property>
 
         <!-- Enable Hibernate's automatic session context management -->
         <property name="current_session_context_class" >thread</ property>
 
         <!-- Echo all executed SQL to stdout -->
         <property name="show_sql" >true</ property>
 
         <!-- Drop and re-create the database schema on startup -->
         <property name="hbm2ddl.auto" >create</ property>
 
         <mapping resource="user.xml" />
 
     </session-factory >
 
 </hibernate-configuration >



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