1. 程式人生 > >搭建ssm框架項目基本原理和主要的配置文件小結

搭建ssm框架項目基本原理和主要的配置文件小結

切面 localhost post 適配 res enter pop -h spl

1.springmvc是spring框架的一個模塊,springmvc和spring無需通過中間整合層進行整合。springmvc是一個基於mvc的web框架。mvc的思想大家已經很熟悉了,簡稱“Model-View-Controller”。

下面先簡單介紹下我對spring-mvc的理解。

技術分享圖片

上面這張圖大概說明了springmvc的運行過程,看起來可能有點雲裏霧裏的,總結起來就是下面這些:

  1. 客戶端發起請求到前端控制器(DispatcherServlet).

  2. 前端控制器請求HandlerMappering 查找Handler,可以根據xml配置、註解進行查找。

  3. DispatcherServlet將請求提交到Controller;

  4. Controller調用業務邏輯處理後,返回ModelAndView;

  5. DispatcherServlet查詢一個或多個ViewResoler視圖解析器,找到ModelAndView指定的視圖;

  6. 視圖負責將結果顯示到客戶端。

這裏稍微解釋下常用的幾個組件名稱和作用。

  • 前端控制器(DispatcherServlet):用於接收請求,響應結果

  • 處理器映射器(HandlerMapping):根據請求的url查找Handler(三大核心組件之一)

  • 處理器適配器(HandlerAdapter):按照特定的規則去執行Handler(三大核心組件之一)

  • 處理器(Handler):編寫Handler時按照HandlerAdapter的要求去做,這樣適配器才可以去正確執行Handler

  • 視圖解析器(View resolver):進行視圖解析(三大核心組件之一)

  • 視圖(View):包括jsp、pdf等

    在了解了上面的基礎原理後下面來講下幾個主要的配置文件。項目開發前所需要的jar包提前要導入項目工程裏面去。

技術分享圖片

有幾個主要的配置文件,先了解下每個配置文件的作用。

1. web.xml:當服務啟動時首先會去加載web.xml這個資源文件,裏面包括了對前端控制器、亂碼問題等配置。

2.applicatonContext.xml : 一般配置數據源,事物,註解 等。

在這裏我使用的是applicatonContext-*.xml的形式將DAO、Service、Transaction

層分開配置,這樣便於管理

分別為applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml

分開配置時,需要在web.xml中配置上下文位置

3.springmvc.xml: 裏面配置的是控制層的 ,如視圖解析器靜態資源, mvc 文件上傳,攔截器等。

4.SqlMapConfig.xml: 該配置文件為MyBatis的配置文件,裏面無需配置,一切交給spring管理,但是xml文件基礎配置要有。

以下是配置文件代碼:

web.xml

[html] view plain copy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>ssm_boot_crm</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. <welcome-file>index.htm</welcome-file>
  10. <welcome-file>index.jsp</welcome-file>
  11. <welcome-file>default.html</welcome-file>
  12. <welcome-file>default.htm</welcome-file>
  13. <welcome-file>default.jsp</welcome-file>
  14. </welcome-file-list>
  15. <!-- 上下文的位置 -->
  16. <context-param>
  17. <param-name>contextConfigLocation</param-name>
  18. <param-value>classpath:applicationContext-*.xml</param-value>
  19. </context-param>
  20. <!-- Spring的監聽器 -->
  21. <listener>
  22. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  23. </listener>
  24. <!-- POST提交過濾器 UTF-8 -->
  25. <filter>
  26. <filter-name>encoding</filter-name>
  27. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  28. <init-param>
  29. <param-name>encoding</param-name>
  30. <param-value>UTF-8</param-value>
  31. </init-param>
  32. </filter>
  33. <filter-mapping>
  34. <filter-name>encoding</filter-name>
  35. <url-pattern>*.action</url-pattern>
  36. </filter-mapping>
  37. <!-- 前端控制器 -->
  38. <servlet>
  39. <servlet-name>crm</servlet-name>
  40. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  41. <init-param>
  42. <param-name>contextConfigLocation</param-name>
  43. <!-- 此處不配置 默認找 /WEB-INF/[servlet-name]-servlet.xml -->
  44. <param-value>classpath:springmvc.xml</param-value>
  45. </init-param>
  46. <load-on-startup>1</load-on-startup>
  47. </servlet>
  48. <servlet-mapping>
  49. <servlet-name>crm</servlet-name>
  50. <!-- 1:*.do *.action 攔截以.do結尾的請求 (不攔截 jsp png jpg .js .css)
  51. 2:/ 攔截所有請求 (不攔截.jsp) 建議使用此種 方式 (攔截 .js.css .png) (放行靜態資源)
  52. 3:/* 攔截所有請求(包括.jsp) 此種方式 不建議使用 -->
  53. <url-pattern>*.action</url-pattern>
  54. </servlet-mapping>
  55. </web-app>

applicationContext-dao.xml

[html] view plain copy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  17. http://www.springframework.org/schema/task
  18. http://www.springframework.org/schema/task/spring-task-4.0.xsd
  19. http://code.alibabatech.com/schema/dubbo
  20. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  21. <!-- srping框架 配置文件 用於管理數據庫連接池 -->
  22. <!-- 配置 讀取properties文件 db.properties -->
  23. <context:property-placeholder location="classpath:db.properties" />
  24. <!-- 配置 數據源 用於連接數據庫 -->
  25. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  26. <!-- 數據庫驅動 -->
  27. <property name="driverClassName" value="${jdbc.driver}" />
  28. <!-- 連接地址 -->
  29. <property name="url" value="${jdbc.url}" />
  30. <!-- 用戶名 -->
  31. <property name="username" value="${jdbc.username}" />
  32. <!-- 密碼 -->
  33. <property name="password" value="${jdbc.password}" />
  34. </bean>
  35. <!-- 配置 Mybatis的工廠 -->
  36. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  37. <!-- 綁定數據源 -->
  38. <property name="dataSource" ref="dataSource" />
  39. <!-- 配置Mybatis的核心 配置文件所在位置 -->
  40. <property name="configLocation" value="classpath:SqlMapConfig.xml" />
  41. <!-- 配置pojo別名 -->
  42. <property name="typeAliasesPackage" value="com.company.ssm.crm.pojo" />
  43. </bean>
  44. <!-- 配置 1:原始Dao開發 接口實現類 Mapper.xml 三個
  45. 2:接口開發 接口 不寫實現類 Mapper.xml 二個 (UserDao、ProductDao
  46. 、BrandDao。。。。。。。)
  47. 3:接口開發、並支持掃描 cn.itcast.core.dao(UserDao。。。。。) 寫在此包下即可被掃描到
  48. -->
  49. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  50. <property name="basePackage" value="com.company.ssm.crm.dao" />
  51. </bean>
  52. </beans>


applicationContext-service.xml

[html] view plain copy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  17. http://www.springframework.org/schema/task
  18. http://www.springframework.org/schema/task/spring-task-4.0.xsd
  19. http://code.alibabatech.com/schema/dubbo
  20. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  21. <!-- 配置掃描包 掃描 @Service spring代理管理業務層 -->
  22. <context:component-scan base-package="com.company.ssm.crm.service" />
  23. </beans>


applicationContext-transaction.xml

[html] view plain copy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10. <!-- spring 事務管理器 -->
  11. <bean id="transactionManager"
  12. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  13. <!-- 數據源 -->
  14. <property name="dataSource" ref="dataSource" />
  15. </bean>
  16. <!-- 通知 -->
  17. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  18. <tx:attributes>
  19. <!-- 傳播行為 -->
  20. <tx:method name="save*" propagation="REQUIRED" />
  21. <tx:method name="insert*" propagation="REQUIRED" />
  22. <tx:method name="add*" propagation="REQUIRED" />
  23. <tx:method name="create*" propagation="REQUIRED" />
  24. <tx:method name="delete*" propagation="REQUIRED" />
  25. <tx:method name="update*" propagation="REQUIRED" />
  26. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  27. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
  28. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  29. </tx:attributes>
  30. </tx:advice>
  31. <!-- AOP 切面 -->
  32. <aop:config>
  33. <aop:advisor advice-ref="txAdvice"
  34. pointcut="execution(* cn.itcast.core.service.*.*(..))" />
  35. </aop:config>
  36. </beans>


springmvc.xml

[html] view plain copy print?
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  16. http://www.springframework.org/schema/task
  17. http://www.springframework.org/schema/task/spring-task-4.0.xsd
  18. http://code.alibabatech.com/schema/dubbo
  19. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  20. <!-- 加載屬性文件 -->
  21. <context:property-placeholder location="classpath:resource.properties" />
  22. <!-- 配置掃描 器 -->
  23. <context:component-scan base-package="com.company.ssm.crm.controller" />
  24. <!-- 配置處理器映射器 適配器 -->
  25. <mvc:annotation-driven />
  26. <!-- 配置視圖解釋器 jsp -->
  27. <bean id="jspViewResolver"
  28. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  29. <property name="prefix" value="/WEB-INF/jsp/" />
  30. <property name="suffix" value=".jsp" />
  31. </bean>
  32. </beans>


SqlMapConfig.xml

[html] view plain copy print?
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  16. http://www.springframework.org/schema/task
  17. http://www.springframework.org/schema/task/spring-task-4.0.xsd
  18. http://code.alibabatech.com/schema/dubbo
  19. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  20. <!-- 加載屬性文件 -->
  21. <context:property-placeholder location="classpath:resource.properties" />
  22. <!-- 配置掃描 器 -->
  23. <context:component-scan base-package="com.company.ssm.crm.controller" />
  24. <!-- 配置處理器映射器 適配器 -->
  25. <mvc:annotation-driven />
  26. <!-- 配置視圖解釋器 jsp -->
  27. <bean id="jspViewResolver"
  28. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  29. <property name="prefix" value="/WEB-INF/jsp/" />
  30. <property name="suffix" value=".jsp" />
  31. </bean>
  32. </beans>


db.properties

[html] view plain copy print?
  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root


以上就是主要的配置文件的配置,特別註意的是在web.xml中默認加載的資源文件再WEB_INF目錄下,如果的你xml不在的話就要寫清楚的文件路徑,例如寫在src目錄下面就要寫classpath,之前開發過程中忽略了這一點,所以啟動一直報錯找不到資源文件。

主要參考出處

並結合自己的SSM搭建,小結SSM環境搭建,以便記憶,交流和學習,有什麽不妥的地方,歡迎指正、交流

搭建ssm框架項目基本原理和主要的配置文件小結