SSH整合(XML)【理解】【應用】【重點】

1.jar包整合

Struts2:

必備包+struts與spring整合的插件包

基本jar包(11個)

從struts-2.3.7-all.zip資源包中獲取apps目錄下對應jar包

apps\struts2-blank\WEB-INF\lib\*.jar

相關jar包(1+2個)

struts2整合Spring(需要使用)

struts2-spring-plugin-2.3.7.jar

struts2整合Ajax(通常需要使用)

struts2-json-plugin-2.3.7.jar

struts2使用註解開發(根據需求開啓)

struts2-convention-plugin-2.3.7.jar

配置文件:

struts.xml

web.xml

web.xml文件中要添加struts2的核心過濾器,否則無法使 struts攔截請求 Spring3:

核心jar包(4個)

spring-beans-3.2.0.RELEASE.jar

spring-context-3.2.0.RELEASE.jar

spring-core-3.2.0.RELEASE.jar

spring-expression-3.2.0.RELEASE.jar

日誌包(2個)

com.springsource.org.apache.commons.logging-1.1.1.jar

com.springsource.org.apache.log4j-1.2.15.jar

AOP包(4個)

spring-aop-3.2.0.RELEASE.jar

spring-aspects-3.2.0.RELEASE.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

JDBC包(2個)

spring-jdbc-3.2.0.RELEASE.jar

spring-tx-3.2.0.RELEASE.jar

整合ORM框架(1個)

spring-orm-3.2.0.RELEASE.jar

WEB集成(1個)

spring-web-3.2.0.RELEASE.jar

配置文件:

applicationContext.xml

web.xml文件中添加spring的IoC容器加載監聽器

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> log4j.properties log4j.rootLogger=info,console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n Hibernate 核心jar包(1個) hibernate3.jar 必須的jar包(6個) lib\required\目錄下的所有包 antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar javassist-3.12.0.GA.jar jta-1.1.jar slf4j-api-1.6.1.jar jpa的jar包(1個) hibernate-jpa-2.0-api-1.0.1.Final.jar slf4j整合log4j的jar包(1個) (log4j在spring中已導入) slf4j-log4j12-1.7.2.jar 配置: hibernate.cfg.xml Model.hbm.xml 其他Jar包 數據庫連接包 log4j包(已經導入) 2.spring如何整合Web 使用監聽器完成spring環境進入web容器,當web容器啓動時,監聽器將啓動,加載配置中的配置 文件,並將spring上下文對象加載到ServletContext範圍下 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> 3.sturts2與spring整合 A.調試struts2,保障其正常工作 B:將struts與spring整合

在struts中使用spring的Bean的方式共有4中 I.直接創建ApplicationContext對象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserEbi userEbi = (UserEbi) ctx.getBean("userEbi"); 優點:容易掌握 缺點:每個操作都要重新加載配置文件,工作量巨大,並且加載的配置只能使用一次 解決方案一:將該對象設置爲類級別屬性或更高級別的屬性 II.將ApplicationContext對象放置到更大的範圍中,例如web環境中 依賴web.xml文件中的監聽器完成該工作 ServletContext sc = ServletActionContext.getServletContext(); WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc); 優點:解決了ApplicationContext對象的多次加載問題 缺點:操作過於繁瑣 解決方案二:能不能不寫該操作 III.使用struts2與spring整合的插件包 基於插件包中的配置<constant name="struts.objectFactory" value="spring" /> 此時Struts對象的屬性可以使用spring的自動裝配模式,默認按照name裝配 struts.objectFactory.spring.autoWire = name 此時struts的對象工廠由Spring進行控制,需要的屬性spring自動裝配/注入 優點:操作模式及其簡潔 缺點:目前Struts的Action類的對象是由struts控制創建的 解決方案三:將struts的對象交由spring 管理 IV.將Action配置爲Bean 將Action類配置成Spring的Bean struts.xml中修改Action類的class屬性爲Bean的名稱,該名稱稱爲僞類名 此時Spring管理Action類 優點:所有對象集中在spring的IoC容器中進行管理 缺點:沒有缺點 注意:ActionBean配置時,千萬要記得配置scope=”prototype” 4.hibernate3與spring整合 整合Hibernate有兩種格式 引入式整合(少用) 獨立整合(主流) 引入式整合: A.DAO類的製作要求繼承HibernateDaoSupport 該類需要注入SessionFactory,注入setter方法已經由父類定義,無需手工書寫 B.在DAO類中注入SessionFactory,首先聲明Bean <!-- Dao --> <bean id="userDao" class="cn.itcast.ssh.user.dao.impl.UserImpl"> <!--注入SessionFactory --> <property name="sessionFactory" ref=" sessionFactory "/> </bean> C.聲明SessionFactory對應的Bean <!-- SessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean> D.Hibernate模板默認沒有自動的事務提交,因此需要手工添加事務,使用註解格式爲業務層接口@Transactional public interface UserEbi { public void add(UserModel um); } E.使用註解事務必須開啓註釋事務支持 applicationContext.xml文件中開啓該註解驅動支持 <!-- 開啓註釋事務驅動 --> <tx:annotation-driven transaction-manager=" txManager "/> F.需要爲註解驅動支持指定具體的事務管理器,使用Hibernate專用的事務管理器 <!-- txManager --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> 獨立整合方式 將hibernate.cfg.xml中的配置信息搬家到applicationContext.xml文件中 <!-- SessionFactory -->

<!-- 獨立整合 -->

<!-- 將hibernate.cfg.xml文件中的配置信息搬家到這裏 -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 數據庫連接的配置 -->

<property name="dataSource" ref="dataSource"/>

<!-- 可選配置 -->

<property name="hibernateProperties">

<props>

<prop key="hibernte.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernte.show_sql">true</prop>

<prop key="hibernte.format_sql">true</prop>

</props>

</property>

<!-- 資源註冊 -->

<property name="mappingResources">

<list>

<value>cn/itcast/ssh/user/vo/UserModel.hbm.xml</value>

</list>

</property>

</bean>

<!-- 將數據庫連接的配置轉換爲dataSource -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://localhost:3306/ssh"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean>

獨立整合常用資源註冊的格式

格式二:

<property name="mappingDirectoryLocations">

<list>

<value>classpath:cn/itcast/ssh</value>

</list>

</property>

格式三:推薦使用此格式(通配符格式)

<property name="mappingLocations">

<list>

<value>classpath:cn/itcast/ssh/*/vo/*Model.hbm.xml</value> </list>

</property>

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