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

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


  
  
  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


  
  
  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


  
  
  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


  
  
  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


  
  
  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


  
  
  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环境搭建,以便记忆,交流和学习,有什么不妥的地方,欢迎指正、交流

 

猜你喜欢

转载自blog.csdn.net/qq_42239765/article/details/83545797