S2SH框架整合之主配置文件的編寫

Web.xml中監聽器的作用是監聽servletContext對象的創建,web中最大的域,此對象創建則代表項目啓動了,項目啓動就要加載spring配置文件
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans";
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:p="http://www.springframework.org/schema/p";
    xmlns:aop="http://www.springframework.org/schema/aop"; xmlns:context="http://www.springframework.org/schema/context";
    xmlns:tx="http://www.springframework.org/schema/tx";
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd";>
    
    <!-- 本程序採用註解的方式開發,比較簡單 -->
    <!-- 1.開啓註解,spring註解,後面還要開啓事務註解-->
    <context:component-scan base-package="com.ruicai.ssh.*"/>
    
    <!-- 2.dataSource配置,首先配置數據庫的配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 使用dpcp數據源 -->
    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${jdbc.driver}"/>
       <property name="url" value="${jdbc.url}"/>
       <property name="username" value="${jdbc.username}"/>
       <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- 3.sqlSessionFactory.Hibernate持久層框架需要這個,在spring-orm的jar包中-->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <!-- mappingLocation和mappingResources二選其一 -->
       <!-- <property name="mappingLocations" value="classpath:com/ruicai/dsx/entity/*.hbm.xml"/> -->
       <property name="mappingResources">
           <list>
               <value>com/ruicai/ssh/domain/Userinfo.hbm.xml</value>
           </list>
       </property>
       <!-- 將Hibernate的一些配置屬性加入到spring的主配置文件中 -->
       <property name="hibernateProperties">
           <props>
               <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
               <prop key="show_sql">true</prop>
               <prop key="format_sql">true</prop>
           </props>
       </property>
    </bean>
    
    <!-- 4.HibernateTemplate配置 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
       <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- 5.spring事務管理配置,並開啓事務註解掃描功能 有兩個選擇,一是針對Hibernate的事務在spring-ormjar包中,
       二是針對數據庫的事務,在spring-jdbc的jar包中-->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       <!-- 既然是Hibernate的事務,則裏面要配置SessionFactory,如果是針對jdbc則是配置dataSource屬性 -->  
       <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd";>
<struts>
    <package name="dsx" namespace="/" extends="struts-default">
       <!-- package的namespace屬性和action的name屬性共同構成了url的地址 -->
       <action name="user_*" class="userinfoAction" method="{1}">
           <!-- name代表方法的返回結果,由此指定跳轉的頁面,type代表跳轉方式, -->
           <result name="user_{1}" type="dispatcher">user_{1}.jsp</result>
       </action>
    </package>
</struts>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns="http://java.sun.com/xml/ns/javaee"; xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"; id="WebApp_ID" version="3.0">
  <display-name>Day1121_SSH2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置 struts過濾器,所有的請求都由該過濾器管理 -->
    <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>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>


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