第一章、MVC框架整合思想

1. 搭建Web运行环境
  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. <version>3.1.0</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>javax.servlet</groupId>
  9. <artifactId>jstl</artifactId>
  10. <version>1.2</version>
  11. </dependency>
  12. <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
  13. <dependency>
  14. <groupId>javax.servlet.jsp</groupId>
  15. <artifactId>javax.servlet.jsp-api</artifactId>
  16. <version>2.3.1</version>
  17. <scope>provided</scope>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-web</artifactId>
  22. <version>5.1.14.RELEASE</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-core</artifactId>
  27. <version>5.1.14.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-beans</artifactId>
  32. <version>5.1.14.RELEASE</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-tx</artifactId>
  37. <version>5.1.14.RELEASE</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-jdbc</artifactId>
  42. <version>5.1.14.RELEASE</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.mybatis</groupId>
  46. <artifactId>mybatis-spring</artifactId>
  47. <version>2.0.2</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>com.alibaba</groupId>
  51. <artifactId>druid</artifactId>
  52. <version>1.1.18</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>mysql</groupId>
  56. <artifactId>mysql-connector-java</artifactId>
  57. <version>5.1.48</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.mybatis</groupId>
  61. <artifactId>mybatis</artifactId>
  62. <version>3.4.6</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>junit</groupId>
  66. <artifactId>junit</artifactId>
  67. <version>4.11</version>
  68. <scope>test</scope>
  69. </dependency>
  70. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-context</artifactId>
  74. <version>5.1.4.RELEASE</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-aop</artifactId>
  79. <version>5.1.14.RELEASE</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.aspectj</groupId>
  83. <artifactId>aspectjrt</artifactId>
  84. <version>1.8.8</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.aspectj</groupId>
  88. <artifactId>aspectjweaver</artifactId>
  89. <version>1.8.3</version>
  90. </dependency>
  91. <dependency>
  92. <groupId>org.slf4j</groupId>
  93. <artifactId>slf4j-log4j12</artifactId>
  94. <version>1.7.25</version>
  95. </dependency>
  96. <dependency>
  97. <groupId>log4j</groupId>
  98. <artifactId>log4j</artifactId>
  99. <version>1.2.17</version>
  100. </dependency>

2. 为什么要整合MVC框架
  1. 1. MVC框架提供了控制器(Controller)调用Service
  2. DAO ---》 Service
  3. 2. 请求响应的处理
  4. 3. 接受请求参数 request.getParameter("")
  5. 4. 控制程序的运行流程
  6. 5. 视图解析 (JSP JSON Freemarker Thyemeleaf )

3. Spring可以整合那些MVC框架
  1. 1. struts1
  2. 2. webwork
  3. 3. jsf
  4. 4. struts2
  5. 5. springMVC

4.Spring整合MVC框架的核心思路

1. 准备工厂
  1. 1. Web开发过程中如何创建工厂
  2. ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
  3. WebXmlApplicationContext()
  4. 2. 如何保证工厂唯一同时被共用
  5. 被共用:Web request|session|ServletContext(application)
  6. 工厂存储在ServletContext这个作用域中 ServletContext.setAttribute("xxxx",ctx);
  7. 唯一:ServletContext对象 创建的同时 ---》 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
  8. ServletContextListener ---> ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
  9. ServletContextListener ServletContext对象创建的同时,被调用(只会被调用一次) ,把工厂创建的代码,写在ServletContextListener中,也会保证只调用
  10. 一次,最终工厂就保证了唯一性
  11. 3. 总结
  12. ServletContextListener(唯一)
  13. ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
  14. ServletContext.setAttribute("xxx",ctx) (共用)
  15. 4. Spring封装了一个ContextLoaderListener
  16. 1. 创建工厂
  17. 2. 把工厂存在ServletContext
  1. ContextLoaderListener使用方式
  2. web.xml
  3. <listener>
  4. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  5. </listener>
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:applicationContext.xml</param-value>
  9. </context-param>

2. 代码整合
  1. 依赖注入:把Sevice对象注入个控制器对象。

image.png

第二章、Spring与Struts2框架整合 (选学)

1. Spring与Struts2整合思路分析
  1. 1. Struts2中的Action需要通过Spring的依赖注入获得Service对象。

2. Spring与Struts2整合的编码实现
  • 搭建开发环境

    • 引入相关jar (Spring Struts2)

      1. <dependency>
      2. <groupId>org.apache.struts</groupId>
      3. <artifactId>struts2-spring-plugin</artifactId>
      4. <version>2.3.8</version>
      5. </dependency>
    • 引入对应的配置文件

      • applicationContext.xml
      • struts.xml
      • log4j.properties
    • 初始化配置 ```xml org.springframework.web.context.ContextLoaderListener

contextConfigLocation classpath:applicationContext.xml

struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2 /*

  1. - Spring (ContextLoaderListener —> Web.xml)
  2. - Struts2(Filter —> Web.xml)
  3. - 编码
  4. - 开发Service对象
  5. ```markdown
  6. 最终在Spring配置文件中创建Service对象
  7. <bean id="userService" class="com.baizhi.struts2.UserServiceImpl"/>
  • 开发Action对象

    • 开发类 ```java public class RegAction implements Action{ private UserService userService; set get

    public String execute(){ userService.register(); return Action.SUCCESS; } }

    1. - Spring (applicationContext.xml)
    2. ```markdown
    3. <bean id="regAction" class="com.baizhi.struts2.RegAction" scope="prototype">
    4. <property name="userService" ref=""/>
    5. </bean
    • Struts2(struts.xml) ```xml url reg.action —-> 会接受到用户的请求后,创建RegAction这个类的对象 进行相应的处理

    /index.jsp ```

3. Spring+Struts2+Mybatis整合(SSM)

1. 思路分析
  1. SSM = Spring+Struts2 Spring+Mybatis

image.png

2. 整合编码
  • 搭建开发环境
    • 引入相关jar (Spring Struts2 Mybatis) ```xml
  1. <dependency>
  2. <groupId>org.apache.struts</groupId>
  3. <artifactId>struts2-spring-plugin</artifactId>
  4. <version>2.3.8</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>javax.servlet</groupId>
  8. <artifactId>javax.servlet-api</artifactId>
  9. <version>3.1.0</version>
  10. <scope>provided</scope>
  11. </dependency>
  12. <dependency>
  13. <groupId>javax.servlet</groupId>
  14. <artifactId>jstl</artifactId>
  15. <version>1.2</version>
  16. </dependency>
  17. <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
  18. <dependency>
  19. <groupId>javax.servlet.jsp</groupId>
  20. <artifactId>javax.servlet.jsp-api</artifactId>
  21. <version>2.3.1</version>
  22. <scope>provided</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-web</artifactId>
  27. <version>5.1.14.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-core</artifactId>
  32. <version>5.1.14.RELEASE</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-beans</artifactId>
  37. <version>5.1.14.RELEASE</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-tx</artifactId>
  42. <version>5.1.14.RELEASE</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-jdbc</artifactId>
  47. <version>5.1.14.RELEASE</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.mybatis</groupId>
  51. <artifactId>mybatis-spring</artifactId>
  52. <version>2.0.2</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.alibaba</groupId>
  56. <artifactId>druid</artifactId>
  57. <version>1.1.18</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>mysql</groupId>
  61. <artifactId>mysql-connector-java</artifactId>
  62. <version>5.1.48</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.mybatis</groupId>
  66. <artifactId>mybatis</artifactId>
  67. <version>3.4.6</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>junit</groupId>
  71. <artifactId>junit</artifactId>
  72. <version>4.11</version>
  73. <scope>test</scope>
  74. </dependency>
  75. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-context</artifactId>
  79. <version>5.1.4.RELEASE</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.springframework</groupId>
  83. <artifactId>spring-aop</artifactId>
  84. <version>5.1.14.RELEASE</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.aspectj</groupId>
  88. <artifactId>aspectjrt</artifactId>
  89. <version>1.8.8</version>
  90. </dependency>
  91. <dependency>
  92. <groupId>org.aspectj</groupId>
  93. <artifactId>aspectjweaver</artifactId>
  94. <version>1.8.3</version>
  95. </dependency>
  96. <dependency>
  97. <groupId>org.slf4j</groupId>
  98. <artifactId>slf4j-log4j12</artifactId>
  99. <version>1.7.25</version>
  100. </dependency>
  101. <dependency>
  102. <groupId>log4j</groupId>
  103. <artifactId>log4j</artifactId>
  104. <version>1.2.17</version>
  105. </dependency>
  1. - 引入对应的配置文件
  2. - applicationContext.xml
  3. - struts.xml
  4. - log4j.properties
  5. - xxxxMapper.xml
  6. - 初始化配置
  7. - Spring (ContextLoaderListener —> Web.xml)
  8. - Struts2(Filter —> Web.xml)
  9. ```xml
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath:applicationContext.xml</param-value>
  16. </context-param>
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>struts2</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  • 编码
    • DAO (Spring+Mybatis) ```markdown
  1. 配置文件的配置
    1. DataSource
    2. SqlSessionFactory ——> SqlSessionFactoryBean
      1. dataSource
      2. typeAliasesPackage
      3. mapperLocations
    3. MapperScannerConfigur —-> DAO接口实现类
  2. 编码

    1. entity
    2. table
    3. DAO接口
    4. 实现Mapper文件 ```
    • Service (Spring添加事务) ```markdown
  3. 原始对象 —-》 注入DAO
  4. 额外功能 —-》 DataSourceTransactionManager —-> dataSource
  5. 切入点 + 事务属性 @Transactional(propagation,readOnly…)
  6. 组装切面 <tx:annotation-driven ```

    • Controller (Spring+Struts2) ```markdown
  7. 开发控制器 implements Action 注入Service
  8. Spring的配置文件
    1. 注入 Service
    2. scope = prototype
  9. struts.xml ```

4. Spring开发过程中多配置文件的处理
  1. Spring会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理及维护。
  2. DAO ------ applicationContext-dao.xml
  3. Service --- applicationContext-service.xml
  4. Action --- applicationContext-action.xml
  5. 注意:虽然提供了多个配置文件,但是后续应用的过程中,还要进行整合
  • 通配符方式 ```markdown
  1. 非web环境 ApplicationContext ctx = new ClassPathXmlApplicationContext(“/applicationContext-*.xml”);
  2. web环境
    1. <param-name>contextConfigLocation</param-name>
    2. <param-value>classpath:applicationContext-*.xml</param-value>
    ```
  1. 非web环境 ApplicationContext ctx = new ClassPathXmlApplicationContext(“/applicationContext.xml”);
  2. web环境
    1. <param-name>contextConfigLocation</param-name>
    2. <param-value>classpath:applicationContext.xml</param-value>
    ```