SpringMVC-01-基本组件与注解式编程

来源:互联网 发布:怎么查看淘宝客pid 编辑:程序博客网 时间:2024/06/15 17:13

一.SpringMVC框架结构

SpringMVC框架处理一次请求的过程:

这里写图片描述

1.1 SpringMVC框架中重要的组件:

DispatcherServlet(前端控制器)

用户请求到达前端控制器,它就相当于MVC模式中的control部分,DispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,DispatcherServlet的存在降低了组件之间的耦合性。

HandlerMapping(处理器映射器)

HandlerMapping负责根据用户请求url找到Handler即处理器,SpringMVC提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。

Handler(处理器)

Handler是继DispatcherServlet前端控制器的后端控制器,在DispatcherServlet的控制下Handler对具体的用户请求进行处理。Handler由开发人员实现

HandlAdapter(处理器适配器)

通过HandlerAdapter执行处理器即Handler,这是适配器模式的应用,通过扩展适配器可以执行更多类型的处理器。

ViewResolver(视图解析器)

ViewResolver负责将处理结果生成View视图,ViewResolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。

View(视图)

SpringMVC框架提供了很多的View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。我们最常用的视图就是jsp。

1.2 SpringMVC组件使用详解:

在以上的各部分中,HandlerMapping处理器映射器、HandlAdapter处理器适配器和ViewResolver视图解析器称为SpringMVC的三大组件。

三大组件的配置

默认配置:

在SpringMVC的核心包org.springframework:spring-webmvc下有一配置文件,DispatcherServlet.properties,其对HandlerMapping,HandlerAdapter和ViewResolver进行了配置,指定了具体使用SpringMVC框架提供的那个类来完成相应的功能,所以如果开发者不自己指定这3个组件,则使用此配置文件中的默认配置的组件。
DispatcherServlet.properties文件:

# Default implementation classes for DispatcherServlet's strategy interfaces.# Used as fallback when no matching beans are found in the DispatcherServlet context.# Not meant to be customized by application developers.org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolverorg.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver//默认HandlerMapping处理器映射器为DefaultAnnotationHandlerMapping类和BeanNameUrlHandlerMapping类org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\    org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping//默认HandlerAdapter处理器适配器为HttpRequestHandlerAdapter类,AnnotationMethodHandlerAdapter类和SimpleControllerHandlerAdapter类org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterorg.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\    org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\    org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolverorg.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslatororg.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolverorg.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager

使用注解开发:

注解开发时使用@ResquestMapping注解进行URL的映射,需要将处理器映射器配置为RequestMappingHandlerMapping,同时方法执行时处理器适配器也可以使用@ResquestMapping注解来进行方法调用,前提是将处理器映射器配置为支持注解开发的RequestMappingHandlerAdapter

注解开发需在springmvc.xml文件中添加配置:

<!-- 配置处理器映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /><!-- 配置处理器适配器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

也可以采用更加简便的方式:
使用注解驱动,SpringMVC会自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter

<mvc:annotation-driven>
原创粉丝点击