SpringMVC_总结_01_配置文件(XML)
一、前言
这一节主要讲解SprinMVC的XML配置方式。
二、配置web.xml
需要在配置部署文件描述符(web.xml)中配置 DispatcherServlet

<!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- <load-on-startup>1</load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
当没有通过init-param来指定contextConfigLocation时,则采用默认的配置文件:WEB-INF/[servletName]-servlet.xml
上述配置指定Spring配置文件在根目录下,并且指定 DispatcherServlet 将会拦截所有请求。
二、Spring配置文件
SpringMVC相关配置:
(1)配置注解驱动
(2)配置自动扫描包
(3)配置静态资源解析器
(4)配置视图解析器
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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--1.配置注解驱动 --> <mvc:annotation-driven /> <!--2.配置自动扫描包 --> <context:component-scan base-package="test.SpringMVC"/> <!--3.配置静态资源解析器 --> <mvc:default-servlet-handler /> <!--4.配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
三、参考资料