SpringMVC学习笔记(二)--三个配置文件

这篇笔记主要记录我对springMVC三个重要配置文件的认识,持续更新。

1、web.xml

在本项目中,我倾向于使用xml配置,而不是按照书上所建议的使用java配置。web.xml相当于项目在服务器的配置文件,是给服务器看的。web.xml向服务器描述了应用程序的根上下文servletfilterlistener标签库等配置信息。因此,它并不是SpringMVC应用独有的的配置文件,以我的经验来看,绝大部分java web应用都必须包含这个配置文件。
在典型的springMVC应用中,我们有一个很重要的组件DispatcherServlet,即上篇讲到的前端控制器。如果你倾向于使用xml配置而不是java配置(我个人更倾向xml配置)的话,那么就需要在web.xml中注册DispatcherServlet。同样,项目的根上下文配置,一些你自定义的filter、servlet,甚至引用的jsp标签库都需要在web.xml中进行注册。下面是一个典型的web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <!-- 设置根上下文配置文件位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <!-- 注册ContextLoaderListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 注册dispatcherServlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 设置servlet到请求的映射 -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 标签库配置 -->
    <jsp-config>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
            <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

2、DispatcherServlet.xml

说到它就不得不同时提到applicationContext.xml。在SpringMVC应用中,通常会有两个应用上下文,一个由DispatcherServlet创建,另一个由ContextLoaderListener创建,在web.xml中可以找到这两个类的配置信息。以下是我摘录的书上原文我们希望DispatcherServlet加载包含web组件的bean,如Controllers、视图解析器以及处理器映射,而ContextLoaderListener要加载应用中的其他bean,这些bean通常是驱动应用程序后端的中间层和数据层组件。
我认为这个约定很重要,至少对一个风格良好的web应用来说很重要。如果你不按照这个约定来配置bean,有时甚至会导致应用无法正常运行,比如把Controller配置在了applicationContext.xml里。下面是一个典型的dispatcher-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    </bean>

    <context:component-scan base-package="Controllers"/>

    <!-- 释放静态资源 -->
    <mvc:annotation-driven/>
    <mvc:resources mapping="/Resources/**" location="/Resources/"/>
</beans>

3、applicationContext.xml

这个配置文件我目前还使用的不多,因为最近还在集中于前端的开发,而开发的web组件都配置在dispatcher-servlet.xml中。以下是我的applicationContext.xml配置(目前只是启用了两个组件扫描):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">


    <context:component-scan base-package="Models"/>
    <context:component-scan base-package="Servlets"/>




</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值