SSH整合,必出精品



SSH:顧名思義(spring,struts2,hirbernate)

     Struts(表示層)+Spring(業務層)+Hibernate(持久層)

Struts是一個表示層框架,主要作用是界面展示,接收請求,分發請求。在MVC框架中,Struts屬於VC層次,負責界面表現,負責MVC關系的分發。(View:沿用JSP,HTTP,Form,Tag,Resourse ;Controller:ActionServlet,struts-config.xml,Action) Hibernate: Hibernate是一個持久層框架,它只負責與關系數據庫的操作。

Spring: Spring是一個業務層框架,是一個整合的框架,能夠很好地黏合表示層與持久層。


前提條件:(引入三大框架的jar包)

第一步:搭建分層(以購買股票為案例)

          1.搭建beans層,構建實體類,以及hirbernate的小配置文件(.hbm.xml)

     

     

     2.搭建dao層以及dao的實現

     

     

     3.構建service進行業務的處理以及dao的植入

       

       

第二步:構架配置文件applicationContext.xml(關聯hirbernate以及struts的配置)    

<?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:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 配置dao -->
    <bean id="stockdao" class="cn.hq.dao.impl.StockDaoImpl">
    	<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 配置service -->
    <bean id="service" class="cn.hq.service.impl.StockServiceImpl">
    	<property name="stockDao" ref="stockdao"></property>
    </bean>
    
    <!-- 配置jdbc連接 -->
    <context:property-placeholder location="classpath:jdb.properties"/>
    
    <!-- 配置數據源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   		<property name="driverClass" value="${jdbc.driver}"></property> 
   		<property name="jdbcUrl" value="${jdbc.url}"></property>
   		<property name="user" value="${jdbc.user}"></property>
   		<property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--hirbernate SessionFactory配置 --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 數據源 --> <property name="dataSource" ref="dataSource"></property> <!--各種hibernate屬性 --> <property name="hibernateProperties"> <props> <!-- 方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop> </props> </property> <!-- 引入小配置 --> <!-- <property name="mappingResources" value="cn/hq/beans/Stock.hbm.xml"></property> --> <property name="mappingDirectoryLocations" value="classpath:cn/hq/beans"></property> </bean>

   <!-- struts2 配置action scope 范圍--> <bean id="addStock" class="cn.hq.action.AddStockAction" scope="prototype"> <property name="service" ref="service"></property> </bean>  
    <!-- 事務配置 -->
    <!-- 注冊事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    	<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <tx:advice id="txAdive" transaction-manager="transactionManager">
    	<tx:attributes>
    		<!-- 指定方法的事務隔離機制以及傳播行為 -->
    		<tx:method name="addStock" isolation="DEFAULT" propagation="REQUIRED"/>
    	</tx:attributes>
    </tx:advice>
    
    <aop:config>
    	<aop:pointcut expression="execution(* *..service.*.*(..))" id="stockpointcut"/>
    	<aop:advisor advice-ref="txAdive" pointcut-ref="stockpointcut"/>
    </aop:config>
    
</beans>

構建jdb.properties,連接數據庫。


 

測試類:

首先測試hirbernate整合

 


struts2整合:

 前提: 必須在Web應用啟動時,創建Spring的ApplicationContext實例 
 步驟 采用ContextLoaderListener來創建ApplicationContext: contextConfigLocation /WEB-INF/spring-config/applicationContext.xml org.springframework.web.context.ContextLoaderListener
             

  構建action類

  

struts.xml配置文件:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    	<package name="default" extends="struts-default">
    		<!-- 定義action節點      指定action    指定方法 -->
    		<action name="addaction" class="cn.hq.action.AddStockAction" method="add">
    			<result name="success">/index.jsp</result>
    		</action>	
    	</package>
    </struts>

 applicationContext.xml中配置:

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></display-name>	
  
   <!--指定配置文件的位置和名稱  -->
  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

   <!-- 監聽器:作用是在整個網站運行的時候,獲取到ServletContext(application)初始化的時候,自動
    注入Spring容器
    -->
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   
  <filter>
     <filter-name>struts</filter-name>
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

測試頁面:

add.jsp

 

 


 

成功沒有奇跡,只有軌跡。

       ---告誡自己

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM