1. 程式人生 > >SSH三大框架整合後的配置檔案詳解

SSH三大框架整合後的配置檔案詳解

學習三大框架Struts2 、 Hibernate 、 Spring時,涉及到三大框架的配置檔案以及整合。今天就來詳細寫寫三大框架配置檔案的詳細內容。

一 Spring的applicationContext.xml中的配置

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

      http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

           http://www.springframework.org/schema/tx

           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- 配置由Spring IoC容器託管物件對應的被註解的類所在的包 -->

<context:component-scanbase-package="com.leipengzj"/>

<!-- 配置通過自動生成代理類實現AOP功能 -->

<aop:aspectj-autoproxy/>

<!--DateSource 使用c3p0連線池-->

<beanid="dateSource"class="com.mchange.v2.c3p0.ComboPooledDateSource"destroy-method="close">

<propertyname="driverClass"value="com.mysql.jdbc.Driver"></property>

<propertyname="jdbcUrl"value="jdbc:mysql:/3306/leiepngzj_table"></property>

<propertyname="user"value="root"></property>

<propertyname="password"value="root"></property>

</bean>

<!-- SessionFactory 配置  配置Spring提供的支援註解ORM對映的Hibernate會話工廠-->

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation">

<propertyname="dateSource"ref="dateSource"></property>

<propertyname="congifLocation"value="classpath:hibernate.cfg.xml"></property>

</bean>

<!-- 配置spring基於註解的宣告式事務管理 -->

<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<!-- 通過setter注入Hibernate會話工廠 -->

<propertyname="sessionFactory"ref="sessionFactory"></property>

</bean>

<tx:annotation-driventransaction-manager="transactionManager"/>

</beans>

二 Hibernate.cfg.xml配置:

<?xmlversion='1.0'encoding='UTF-8'?>

<!DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->

<hibernate-configuration>

<session-factory>

<!-- 資料庫連線資訊 ,其他的寫到了spring配置檔案中去 -->

       <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 其他配置 -->

<propertyname="show_sql">true</property>

       <propertyname="hbm2ddl.auto">update</property>

<!-- 匯入對映配置 -->

<mappingresource="../../.hbm.xml"/>

<mappingresource="../../.hbm.xml"/>

<mappingresource="../../.hbm.xml"/>

</session-factory>

</hibernate-configuration>

三 struts.xml中的配置檔案:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstrutsPUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<!-- 配置為開發模式 -->

<constantname="struts.devMode"value="true"></constant>

<!-- 配置副檔名為action -->

<constantname="struts.action.extension"value="action"></constant>

<!-- 當struts配置檔案修改時,系統是否自動重新載入該檔案,預設false,開發階段最好開啟 -->

<constantname="struts.configuration.xml.reload"value="true"></constant>

<!-- 全域性檢視 -->

<packagename="LunTanSystem"extends="struts-default">

<global-results>

<resultname="error">/error.jsp</result>

<resultname="ajax">/ajax.jsp</result>

<resultname="noLogin">/login.jsp</result>

</global-results>

<!-- 當與action整合後,class屬性寫的就是Spring中bean的屬性 -->

<actionname="test"class="testAction">

<resultname="success">/test.jsp</result>

</action>

</package>

</struts>

四 web.xml配置檔案的編寫:

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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>

<!-- 配置Struts2的主過濾器 -->

<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的監聽器,用於初始化ApplicationContext.xml  -->

<listener>

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

</listener>

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:applicationContext*.xml</param-value>

</context-param>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>