Spring MVC基于模型-视图-控制器(Model-View-Controller,MVC)实现,能够帮助我们构建像Spring框架那样灵活和松耦合的Web应用程序。
1,跟踪Spring MVC的请求
请求的第一站是Spring的DispatcherServlet。与大多数基于Java的Web框架一样,Spring MVC所有的请求都会通过一个前端控制器Servlet。前端控制器是常用的Web应用程序模式,在这里一个单实例的Servlet将请求委托给应用程序的其他组件来执行实际的处理。在Spring MVC中,DispatcherServlet就是前端控制器。
DispatcherServlet的任务是将请求发送给Spring MVC控制器。控制器是一个用于处理请求的Spring组件。在典型的应用程序中可能会有多个控制器,DispatcherServlet需要知道应该将请求发送给哪个控制器。所以DispatcherServlet会查询一个或多个处理器映射来确定请求的下一站在哪里。处理器映射会根据请求所携带的URL信息来进行决策。
一旦选择了合适的控制器,DispatcherServlet会将请求发送给选中的控制器。到达了控制器,请求会卸下其负载(用户提交的信息)并耐心等待控制器处理这些信息。
控制器在完成逻辑处理后,通常会产生一些信息,这些信息需要返回给用户并在浏览器上显示。这些信息被称为(model)。不过仅仅给用户返回原始的信息是不够的——这些信息需要以用户友好的方式进行格式化,一般是HTML。所以信息需要发送给一个试图(View)。通常会是JSP。
控制器所做的最后一件事是将模型数据打包,并且标示出用于渲染输出的试图名称。它接下来会将请求连同模型和视图名称发送回DispatcherServlet。
2,搭建Spring MVC
Spring MVC的核心是DispatcherServlet,这个Servlet充当Spring MVC的前端控制器。与其他Servlet一样,DispatcherServlet必须在Web应用程序的web.xml文件中进行配置。所以在应用程序中使用Spring MVC的第一件就是将下面的<servlet>声明放入web.xml。
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
这里要注意的是<servlet-name>的属性配置,这里的<servlet-name>为hello,DispatcherServlet将会尝试从一个名为hello-servlet.xml文件(稍后会做配置)来加载应用上下文。
接下来就是要声明DispatcherServlet处理哪些URL。比较常见的DispatcherServlet匹配模式是*.htm、/*或者/app,但这些模式都存在一些问题,所以一般会采用以下配置:
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
通过将DispatcherServlet映射到/,声明了它会作为默认的servlet并且会处理所有请求,包括对静态资源的请求。
如果想要对静态资源的请求做另外的处理,那么可以在Spring配置文件中对其进行配置,这个配置文件就是上面提到的hello-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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:resources location="/resources/"
mapping="/resources/**"/>
</beans>
<mvc:resources>建立了一个服务于静态资源的处理器。以上配置表明,所有以/resources路径开头的请求都会自动由应用程序根目录下的/resources目录提=提供服务。因此,我们的所有图片,样式表,JavaScript以及其他的静态资源都必须放在应用程序的/resources目录下。
3,配置注解驱动的Spring MVC
前面提到,DispatcherServlet需要咨询一个或多个处理器映射来明确地将请求发送给哪个控制器。Spring自带了多个处理器映射来实现供我们选择,但是我们只需要在hello-servlet.xml文件添加一行就能实现Spring MVC所提供的注解驱动特性:
<?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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/> ——配置注解驱动
<mvc:resources location="/resources/"
mapping="/resources/**"/>
</beans>
4,解析内部视图
上面提到控制器需要对信息进行格式化,要发生一个视图(view),一般会是JSP。假设我们将hello应用程序的所有JSP放在“/WEB-INF/hello/”目录下,就要在hello-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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<mvc:resources location="/resources/" mapping="/resources/**"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/hello/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
如果我们想要返回一个"home"的试图,那么就会自动的在前面加上“/WEB-INF/hello/”的前缀,和“.jsp”的后缀。于是得到一个“/WEB-INF/hello/home.jsp”的一个路径。也就是说我们只要把home.jsp文件放在“/WEB-INF/hello”目录下就可以了。
5,完成Spring应用上下文
DispatcherServlet会根据一个XML文件来加载其Spring应用上下文,而这个文件的名字基于它的<servlet-name>属性来确定。但是如果还有其他的Bean怎么办?我们可能会有多个XML文件,一个用于服务层,一个用于持久层等等,这个时候就是要用到ContextLoaderListener了。ContextLoaderListener是一个监听器,除了DispatcherServlet创建的Spring应用上下文以外,还能加载其他的配置到一个Spring应用上下文中。要使用ContextLoaderListener,需要在web.xml中做添加:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
然后我们可以在<context-param>中添加所需要的xml文件:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/hello-servlet.xml
classpath:spring.xml
classpath:spring-mybatis.xml
</param-value>
</context-param>