请注意,本文编写于 2105 天前,最后修改于 2104 天前,其中某些信息可能已经过时。
Spring MVC 详解
Spring MVC 的概述
Spring MVC 是Spring提供的一个实现了WEB MVC 设计模式的轻量级WEB框架,它与Struts2框架一样,都属于MVC框架,但是其使用和性能方面比Struts2框架更加优异
Spring MVC框架具有如下特点
- 是Spring框架的一部分,可以方便地利用Spring所提供的的其他功能
- 灵活性强,易于和其他框架集成
- 提供了一个前端控制器
DispatcherServlet
,能够使开发人员无须额外开发控制器对象 - 能够自动绑定用户输入,并且能够正确的转换数据类型
- 内置了常见的校验器,可以校验用户的输入,如果校验不能通过,那么就会重定向到输入表单
- 支持国际化,可以根据用户区域显示多国语言
- 支持多种视图技术,它支持JSP\Velocity\FreeMarker等前端视图引擎
- 使用基于XML的配置文件,在编辑后不用重新编译应用程序
基本的配置
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_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!-- 工程编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- log4j配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!--配置springmvc DispatcherServlet-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--配置dispatcher.xml作为mvc的配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<!--为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--把applicationContext.xml加入到配置文件中-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!--welcome pages-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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: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中的配置-->
<!-- 自动扫描装配 -->
<context:component-scan base-package="eendtech.controller"/>
<!--启用spring的一些annotation -->
<context:annotation-config/>
<!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
<mvc:annotation-driven/>
<!--静态资源映射-->
<!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
<!--<mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>-->
<mvc:resources mapping="/js/**" location="/WEB-INF/static/js/"/>
<mvc:resources mapping="/img/**" location="/WEB-INF/static/img/"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/><!--设置JSP文件的目录位置-->
<!--<property name="suffix" value=".jsp"/>-->
<property name="order" value="0"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="eendtech.covert.DateConverter"/>
</set>
</property>
</bean>
<!--<bean id="formatterService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">-->
<!--<property name="formatters">-->
<!--<set>-->
<!--<bean class="eendtech.Formatter.DateFormatter"/>-->
<!--</set>-->
<!--</property>-->
<!--</bean>-->
<!--配置拦截器-->
<mvc:interceptors>
<!--使用Bean直接定义在<mvc:interceptor>下面的interceptor将拦截所有的请求-->
<bean class="eendtech.interceptor.LoginInterceptor"/>
</mvc:interceptors>
</beans>
Spring MVC的工作流程
经过学习,了解到Spring MVC的工作流程大体如下:
- 用户通过客户端向服务器发送请求,请求会被Spring MVC的前端控制器
DispatcherServlet
所拦截 - DispatcherServlet拦截到请求后,会调用HandlerMapping处理器映射器
- 处理器映射器根据请求的URL地址找到具体的处理器,生成处理器对象及处理器拦截器(若果有则生成),一并返回给前端控制器
- 前端控制器
DispatcherServlet
会通过返回信息选择合适的处理器适配器HandlerAdapter
HandlerAdapter
会调用并执行Handler
处理器,这里的出来器指的就是程序中编写的Controller类,也被称之为后端控制器Controller
执行完成之后,会返回一个ModelAndView
对象,该对象中会包含视图名或包含模型和视图名HandlerAdapter
处理器适配器将ModelAndView
对象返回给前端控制器- 前端控制器会根据
ModelAndView
对象选择一个合适的ViewResolver
(视图解析器) ViewResolver
解析之后,会向DispatcherServlet
(前端控制器)中返回具体的View(视图)- 前端控制器对View进行渲染
- 视图渲染结果会返回给客户端浏览器进行显示