zoukankan      html  css  js  c++  java
  • 【Spring】SpringMVC配置文件

    SpringMVC中一般会引入三个配置文件applicationContext.xml、dispatcher-servlet.xml(SpringMVC-servlet.xml)、web.xml

    1.dispatcher-servlet.xml(SpringMVC-servlet.xml)

    DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,默认使用servlet context。加载包含Web组件的bean,如控制器、视图解析器以及处理器映射。

    mvc:annotation-driven
    当配置了mvc:annotation-driven/后,Spring就知道了我们启用注解驱动。然后Spring通过context:component-scan/标签的配置,会自动为我们将扫描到的@Component,@Controller,@Service,@Repository等注解标记的组件注册到工厂中,来处理我们的请求。配置如果没有mvc:annotation-driven/,那么所有的Controller可能就没有解析。

    context:component-scan

        <!-- 扫描controller -->
        <context:component-scan base-package="**.*controller"/>
    
    2.applicationContext.xml

    使用application context,使用SpringMVC可以不配置applicationContext.xml,若使用需要在web.xml中添加Listener。加载应用中的其他bean,通常是驱动应用后端的中间层和数据层组件。

        <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:applicationContext*.xml
            </param-value>
        </context-param>
    
    3.web.xml
        <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
    
        <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:applicationContext*.xml
            </param-value>
        </context-param>
    
        <!-- springMVC的核心控制器 -->
        <servlet>
            <servlet-name>springMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:springMVC-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
            <async-supported>true</async-supported>
        </servlet>
        <servlet-mapping>
            <servlet-name>springMVC</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>
    
  • 相关阅读:
    线程应用:(九)阻塞队列的应用
    线程应用:(八)Semaphere信号灯、CyclicBarrier汇合、CountDownLatch计数器、Exchanger
    线程应用:(七)Lock & Condition同步通信
    线程应用:(六)线程池、Callable与Future
    线程应用:(五)传统线程使用实例
    记一次linux安装mysql
    Pycharm断点调试入门
    django admin后台插件:django-suit入门
    在django中使用django_debug_toolbar
    pyquery详细用法
  • 原文地址:https://www.cnblogs.com/grape1211/p/9575164.html
Copyright © 2011-2022 走看看