• (续)SSM整合之SSM整合笔记(ContextLoaderListener)(P177-178)


    目录

    ContextLoaderListener

    一  ContextLoaderListener

    二   测试ContextLoaderListener

    1 新建模块spring_listener     com.atguigu

    2. 导入依赖

    3 .转web

    4 .web.xml

    5 springmvc.xml

    6 .spring.xml

    7  首页index.html

    8 控制层 HelloController

    9  service接口HelloService 

    10 service实现类

    11   配置到tomcat中并启动

    12 测试结果


    ContextLoaderListener

    一  ContextLoaderListener

    Spring 提供了监听器 ContextLoaderListener ,实现 ServletContextListener 接口,可监听
    ServletContext 的状态,在 web 服务器的启动,读取 Spring 的配置文件,创建 Spring IOC 容器。 web应用中必须在 web.xml 中配置

     

    1. <listener>
    2. <listenerclass>org.springframework.web.context.ContextLoaderListenerlistener-class>
    3. listener>
    4. <context-param>
    5. <param-name>contextConfigLocationparam-name>
    6. <param-value>classpath:spring.xmlparam-value>
    7. context-param>

    二   测试ContextLoaderListener

    1 新建模块spring_listener     com.atguigu

    2. 导入依赖

    war
    
    
        
        
            org.springframework
            spring-webmvc
            5.3.1
        
    
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
    
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
    
        
        
            org.thymeleaf
            thymeleaf-spring5
            3.0.12.RELEASE
        
    
    

    3 .转web

    4 .web.xml

    1. <servlet>
    2. <servlet-name>SpringMVCservlet-name>
    3. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    4. <init-param>
    5. <param-name>contextConfigLocationparam-name>
    6. <param-value>classpath:springmvc.xmlparam-value>
    7. init-param>
    8. <load-on-startup>1load-on-startup>
    9. servlet>
    10. <servlet-mapping>
    11. <servlet-name>SpringMVCservlet-name>
    12. <url-pattern>/url-pattern>
    13. servlet-mapping>
    14. <listener>
    15. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    16. listener>
    17. <context-param>
    18. <param-name>contextConfigLocationparam-name>
    19. <param-value>classpath:spring.xmlparam-value>
    20. context-param>

    5 springmvc.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    7. <context:component-scan base-package="com.atguigu.controller">context:component-scan>
    8. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    9. <property name="order" value="1"/>
    10. <property name="characterEncoding" value="UTF-8"/>
    11. <property name="templateEngine">
    12. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    13. <property name="templateResolver">
    14. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    15. <property name="prefix" value="/WEB-INF/templates/"/>
    16. <property name="suffix" value=".html"/>
    17. <property name="templateMode" value="HTML5"/>
    18. <property name="characterEncoding" value="UTF-8" />
    19. bean>
    20. property>
    21. bean>
    22. property>
    23. bean>
    24. <mvc:annotation-driven />
    25. <mvc:view-controller path="/" view-name="index">mvc:view-controller>
    26. beans>

    6 .spring.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="com.atguigu.service.impl">context:component-scan>
    7. beans>

    7  首页index.html

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. body>
    10. html>

    8 控制层 HelloController

    1. package com.atguigu.controller;
    2. @Controller
    3. public class HelloController {
    4. @Autowired
    5. private HelloService helloService;
    6. }

    9  service接口HelloService 

    1. package com.atguigu.service;
    2. public interface HelloService {
    3. }

    10 service实现类

    1. package com.atguigu.service.impl;
    2. @Service
    3. public class HelloServiceImpl implements HelloService {
    4. }

    11   配置到tomcat中并启动

    12 测试结果

     

  • 相关阅读:
    AI智能外呼okcc呼叫中心外呼的几种形式
    如何使用搜索功能精确筛选数据?
    背包问题学习笔记-分组背包
    资深技术官聊聊Java程序员三年这个“梗”,该如何突破自身瓶颈?
    OpenGL之帧缓冲与片元运算
    MySQL数据库期末考试试题及参考答案(06)
    -数字之和-
    osgEarth示例分析——osgearth_imageoverlay
    Matlab图像处理-从RGB转换为HSV
    【基本数据结构 四】线性数据结构:队列
  • 原文地址:https://blog.csdn.net/m0_59281987/article/details/127992968