DispatcherServlet是Spring MVC的核心。在这里请求会第一次接触到框架,它要负责将请求路由到其他的组件之中。按照传统的方式, 像DispatcherServlet这样的Servlet会配置在web.xml文件中,这个文件会放到应用的WAR包里面。当然,这是配置DispatcherServlet的方法之一。 但是,借助于Servlet 3规范和Spring 3.1的功能增强,这种方式已经不是唯一的方案了。
WebApplicationInitializer是Spring MVC提供的一个接口,用于确保你的实现探测并且自动初始化Servlet 3容器。 WebApplicationInitializer有一个抽象基类实现AbstractDispatcherServletInitializer,实现该抽象类非常简单,继承它,实现servlet映射方法和DispatcherServlet配置的定位方法即可
package com.lf.web.config; import com.lf.web.WebConfig; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; /** * 此配置相当于web.xml此种方法用于指定配置类 * Created by LF on 2017/5/4. */ public class MyJavaConfigDispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class};//指定配置类 } @Override protected String[] getServletMappings() { return new String[]{"/"};//请求映射 } }若是使用基于xml的spring配置, 则是继承AbstractDispatcherServletInitializer
package com.lf.web.config; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; /** * Created by LF on 2017/5/5. */ public class MyXmlConfigDispatcherServlet extends AbstractDispatcherServletInitializer { @Override protected WebApplicationContext createServletApplicationContext() { return null; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } @Override protected WebApplicationContext createRootApplicationContext() { XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocations("classpath:application.xml"); return context; } }基于Java配置和基于XML配置都提供了相似的默认配置,用于覆盖DispatcherServlet默认实现。 之所以这么做,是为了减少重复工作,因为多数应用都使用相同的配置,也是为了提供更高层级的抽象配置,降低学习曲线,使用户仅需要很少的关于配置的背景知识就可以使用Spring MVC了。 MVC基于java的配置比基于XML的配置要更容易些,也有更细粒度的控制。
在@Configuration 注解的类上使用注解@EnableWebMvc即可开启MVC Java配置
若是使用XML达到同样的效果,得在DispatchServlet上下文中使用mvc:annotation-driven元素(如果没有DispatchServlet 环境则是在root context根环境中)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> </beans>这可以运行起来,它的确能够启用Spring MVC,但还有不少问题要解 决: - 没有配置视图解析器。如果这样的话,Spring默认会使 用BeanNameView-Resolver,这个视图解析器会查找ID与视 图名称匹配的bean,并且查找的bean要实现View接口,它以这样 的方式来解析视图 - 没有启用组件扫描。这样的结果就是,Spring只能找到显式声明 在配置类中的控制器。 - 这样配置的话,DispatcherServlet会映射为应用的默认 Servlet,所以它会处理所有的请求,包括对静态资源的请求,如 图片和样式表.
通过调用DefaultServlet-HandlerConfigurer的enable()方法,我们要求DispatcherServlet将对静态资源的请求转发到Servlet容 器中默认的Servlet上,而不是使用DispatcherServlet本身来处理此类请求。
完整的示例代码:https://git.oschina.net/null_064_8008/SpringLearning/tree/master/Springmvc?dir=1&filepath=Springmvc&oid=c6eba9c49dc5f135820c9e44a69b208bec36afa5&sha=c160960cbb322cc775a6840c825411191b8110ff