SSM基本配置

1. 首先從web.xml配置入手

A:  <!-- spring的監(jiān)聽(tīng)器 -->
          <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
    B:  <!--加載配置文件  -->
          <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-*.xml</param-value>
          </context-param>
        
        a:  contextloaderlistener 這個(gè)類被定義為監(jiān)聽(tīng)器,并讀取在參數(shù)  contextConfigLocation中定義的xml文件,
            如果不設(shè)置contextConfigLocation的初始參數(shù)則默認(rèn)會(huì)讀取WEB-INF路徑下的 application.xml文件,如
            果需要自定義了另外的xml 則可以在contextConfigLocation下定義,ContextLoaderListener會(huì)讀取這些
            XML文件并產(chǎn)生 WebApplicationContext對(duì)象,然后將這個(gè)對(duì)象放置在ServletContext的屬性里,這樣我們
            只要可以得到Servlet就可 以得到WebApplicationContext對(duì)象,并利用這個(gè)對(duì)象訪問(wèn)spring 容器管理的bean。
        
        b:  Spring提供ServletContentListener的一個(gè)實(shí)現(xiàn)類ContextLoaderListener監(jiān)聽(tīng)器,該類可以作為L(zhǎng)istener
            使用,在啟動(dòng)Tomcat容器的時(shí)候,該類的作用就是自動(dòng)裝載ApplicationContext的配置信息,如果沒(méi)有設(shè)置
            contextConfigLocation的初始參數(shù)則會(huì)使用默認(rèn)參數(shù)WEB-INF路徑下的application.xml文件。如果需要自
            定義讀取多個(gè)配置文件或者修改默認(rèn)路徑,則可以在web.xml中設(shè)置;ContextLoaderListener會(huì)讀取這些XML
            文件并產(chǎn)生 WebApplicationContext對(duì)象,然后將這個(gè)對(duì)象放置在ServletContext的屬性里,這樣我們只要
            可以得到Servlet就可 以得到WebApplicationContext對(duì)象,并利用這個(gè)對(duì)象訪問(wèn)spring 容器管理的bean。
        
        c:  ServletConfig對(duì)象在Servlet的配置文件中,可以使用一個(gè)或多個(gè)<init-param>標(biāo)簽為servlet配置一些初始化參數(shù)。
            (配置在某個(gè)servlet標(biāo)簽或者整個(gè)web-app下)當(dāng)servlet配置了初始化參數(shù)后,web容器在創(chuàng)建servlet實(shí)例對(duì)象時(shí),
            會(huì)自動(dòng)將這些初始化參數(shù)封裝到ServletConfig對(duì)象中,并在調(diào)用servlet的init方法時(shí),將ServletConfig對(duì)象傳
            遞給servlet。進(jìn)而,程序員通過(guò)ServletConfig對(duì)象就可以得到當(dāng)前servlet的初始化參數(shù)信息。
        
        d:ServletContext對(duì)象
            WEB容器在啟動(dòng)時(shí),它會(huì)為每個(gè)WEB應(yīng)用程序都創(chuàng)建一個(gè)對(duì)應(yīng)的ServletContext對(duì)象,它代表當(dāng)前web應(yīng)用。
            ServletContext對(duì)象應(yīng)用1:多個(gè)web組件之間使用它實(shí)現(xiàn)數(shù)據(jù)共享ServletConfig對(duì)象中維護(hù)了ServletContext
            對(duì)象的引用,開(kāi)發(fā)人員在編寫servlet時(shí),可以通過(guò)ServletConfig.getServletContext方法獲得ServletContext對(duì)象。
            由于一個(gè)WEB應(yīng)用中的所有Servlet共享同一個(gè)ServletContext對(duì)象,因此Servlet對(duì)象之間可以通過(guò)ServletContext
            對(duì)象來(lái)實(shí)現(xiàn)通訊。ServletContext對(duì)象通常也被稱之為context域?qū)ο?  
                
        C:  <!-- POST提交過(guò)濾器 UTF-8 -->
            <filter>
                <filter-name>encoding</filter-name>
                <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
                <init-param>
                    <param-name>encoding</param-name>
                    <param-value>UTF-8</param-value>
                </init-param>
            </filter>
            <filter-mapping>
                <filter-name>encoding</filter-name>
                <url-pattern>*.action</url-pattern>
            </filter-mapping>
        
        D:  <!--springmvc的前端控制器  -->
            <servlet>
                <servlet-name>crm</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>classpath:springmvc.xml</param-value>
                </init-param>
                <!-- 
                    配置“1”,tomcat啟動(dòng)時(shí)就初始化 DispatcherServlet,
                    否則不配置就是第一次訪問(wèn)時(shí)候才初始化DispatcherServlet
                -->
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>crm</servlet-name>
                <!-- 
                    1:*.do *.action 攔截以.do結(jié)尾的請(qǐng)求 (不攔截 jsp png jpg .js .css) 
                    2:/ 攔截所有請(qǐng)求 (不攔截.jsp) 建議使用此種 方式 (攔截 .js.css .png) (放行靜態(tài)資源) 
                    3:/* 攔截所有請(qǐng)求(包括.jsp) 此種方式 不建議使用
                 -->
                <url-pattern>*.action</url-pattern>
            </servlet-mapping>

2. 編寫數(shù)據(jù)源applicationContext-dao.xml

★:src下新建db.properties 內(nèi)容如下
            jdbc.driver=com.mysql.jdbc.Driver
            jdbc.url=jdbc:mysql://localhost:3306/ssm-crm?characterEncoding=utf-8
            jdbc.username=root
            jdbc.password=1234
    A:  <!-- 配置 讀取properties文件 jdbc.properties -->
        <context:property-placeholder location="classpath:db.properties" />

    B:  <!-- 配置 數(shù)據(jù)源 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <!-- 驅(qū)動(dòng) -->
            <property name="driverClassName" value="${jdbc.driver}" />
            <!-- url -->
            <property name="url" value="${jdbc.url}" />
            <!-- 用戶名 -->
            <property name="username" value="${jdbc.username}" />
            <!-- 密碼 -->
            <property name="password" value="${jdbc.password}" />
        </bean>
    
    C:  <!-- 配置 Mybatis的工廠 -->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 數(shù)據(jù)源 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 配置Mybatis的核心 配置文件所在位置 -->
            <property name="configLocation" value="classpath:SqlMapConfig.xml" />
            <!-- 配置pojo別名 -->
            <property name="typeAliasesPackage" value="com.neo.pojo"></property>
        </bean>
    D:  src下創(chuàng)建空文件 SqlMapConfig.xml
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
        <configuration>
        </configuration>
    
    E:  <!-- 配置Mapper掃描器 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.neo.dao" />
        </bean>

3. 編寫applicationContext-service.xml

<!-- 配置  掃描   @Service -->
    <context:component-scan base-package="com.neo.service"/>

4. 編寫applicationContext-trans.xml

?

A:  <!-- 事務(wù)管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 數(shù)據(jù)源 -->
            <property name="dataSource" ref="dataSource" />
        </bean>

    B:  <!-- 通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 傳播行為 -->
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="create*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            </tx:attributes>
        </tx:advice>

    C:  <!-- 切面 -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice"
                pointcut="execution(* com.neo.service.*.*(..))" />
        </aop:config>

5. 配置springmvc.xml

0:  <!-- 加載屬性文件 -->
        <context:property-placeholder location="classpath:resource.properties"/>    
    
    A:  <!-- 配置掃描 器 -->
        <context:component-scan base-package="com.neo.controller"/>
    
    B:  <!-- 配置處理器映射器 和 適配器 -->
        <mvc:annotation-driven/>
    
    C:  <!-- 配置視圖解釋器 jsp -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 對(duì)于java中的思考的方向,1必須要看前端的頁(yè)面,對(duì)于前端的頁(yè)面基本的邏輯,如果能理解最好,不理解也要知道幾點(diǎn)。 ...
    神尤魯?shù)婪?/span>閱讀 901評(píng)論 0 0
  • 對(duì)于三個(gè)框架的結(jié)合使用,導(dǎo)入jar包后需要進(jìn)行寫配置文件,其基本的配置以下做個(gè)示例: 一,web.xml 在使用時(shí)...
    暗香撫動(dòng)閱讀 3,070評(píng)論 0 2
  • 對(duì)于maven的項(xiàng)目構(gòu)建和依賴管理在這里先不做詳細(xì)配置介紹,之后應(yīng)該會(huì)做詳細(xì)的補(bǔ)充,今天先寫寫在maven...
    半雨落殤閱讀 525評(píng)論 3 8
  • SSM整合筆記 整合Spring 編寫xml配置文件,開(kāi)啟注解掃描(指定Controller注解不掃描) <!--...
    EvanPoison閱讀 1,036評(píng)論 0 0
  • 姓名:張漢超 公司:東莞耀升機(jī)電有限公司 組別:4月25-27日六項(xiàng)精進(jìn)245期學(xué)員 【日精進(jìn)打卡第196天】 【...
    張漢超閱讀 184評(píng)論 0 0

友情鏈接更多精彩內(nèi)容