zoukankan      html  css  js  c++  java
  • <mvc:annotation-driven />

    要使用spring mvc中的@Controller注解,就必须要配置<mvc:annotation-driven />,否则org.springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器。

    Spring 3.0.x中使用了annotation-driven后,缺省使用DefaultAnnotationHandlerMapping 来注册handler method和request的mapping关系。 

    AnnotationMethodHandlerAdapter来在实际调用handlermethod前对其参数进行处理。 

    详解:<mvc:annotation-driven />就是

    A。注册了

    一个RequestMappingHandlerMapping,

    一个RequestMappingHandlerAdapter

    和一个ExceptionHandlerExceptionResolver

    (其中包括)支持使用注解标注在Controller方法的处理请求,例如@RequestMapping ,@ExceptionHandler等等

    B。它还执行以下操作:

    1. Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.

    2. Support for   formatting Number fields using the  @NumberFormat annotation through the ConversionService.

    3. Support for   formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation.

    4. Support for   validating @Controller inputs with  @Valid, if a JSR-303 Provider is present on the classpath.

    5. HttpMessageConverter support for  @RequestBody method parameters and  @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.

    C。Details for B5

    This is the complete list of HttpMessageConverters set up by mvc:annotation-driven

    :

    • ByteArrayHttpMessageConverter converts byte arrays.

    • StringHttpMessageConverter converts strings.

    • ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.

    • SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.

    • FormHttpMessageConverter converts form data to/from a  MultiValueMap<String,String>.

    • Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.

    • MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.

    • AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.

    • RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.

    其实相信在大多数实际应用环境中使用mvc:annotation-driven是少数,因为一般都满足不了需求,但想快速搭配环境还是比较适合的.当使用java config,记得有文章介绍不推荐配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter

    spring MVC配置

    spring MVC的配置比较简单,主要配置两处:web.xml和spring自己的配置

    web.xml配置如下。有三处需要注意。

        1. web.xml有两处指定了spring配置文件路径,但作用不同

    context-param中指定的路径创建的是
    通用的spring管理bean,用来管理我们的service层及其以下的资源、事务、aop等。      
    B. 在 servlet 中定义的DispatcherServlet是spring MVC的定义,

    它用来管理web层资源

    。因为是自家东西,集成也比较方便。web层的spring application会继承通用层的spring application,方便我们调用底层服务。

    2.在定义 DispatcherServlet的匹配模式的时候,不再使用常用的"*.action"或"*.do",而是使用"/",表示对所有资源的访问都会进入spring MVC,这样我们访问的时候不需要带后缀,直接以localhost:8080/restbucks/coffee这种url访问。而且后缀我们另做他用:客户端用后缀来标识想要的数据格式, 我们会根据后缀返回 对应格式的 数据。 比如请求的url带".xml"后缀表示你想要XML格式的数据,".json"后缀表示你想要JSON格式的数据,后面会讲到如何配置。

        3.在正常的web项目中,我们都免不了会有jsp页面和静态资源,为了能正常访问,我们需要做一些设置。有好几种解决办法,这里使用最简单,效率也最高的一种。将不需要spring MVC处理的文件后缀在web.xml里显示标识出来,让web服务器的默认servlet处理,不走spring MVC。也可以使用 spring MVC提供的功能。参看 <mvc:resources/>和<mvc:default-servlet-handler/> 的使用。

     1 < context-param >
     2 
     3 < param-name >contextConfigLocation</ param-name >
     4 
     5 < param-value >
     6 
     7 classpath*:applicationContext.xml
     8 
     9 </ param-value >
    10 
    11 </ context-param >
    12 
    13  
    14 
    15 < listener >
    16 
    17 < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
    18 
    19 </ listener >
    20 
    21  
    22 
    23 < servlet >
    24 
    25 < servlet-name >restBucks</ servlet-name >
    26 
    27 < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
    28 
    29 < init-param >
    30 
    31 < param-name >contextConfigLocation</ param-name >
    32 
    33 < param-value >/WEB-INF/restBucks-servlet.xml</ param-value >
    34 
    35 </ init-param >
    36 
    37 < load-on-startup >1</ load-on-startup >
    38 
    39 </ servlet >
    40 
    41 < servlet-mapping >
    42 
    43 < servlet-name >restBucks</ servlet-name >
    44 
    45 < url-pattern >/</ url-pattern >
    46 
    47 </ servlet-mapping >
    48 
    49  
    50 
    51 <!--default mapping-->
    52 
    53 < servlet-mapping >
    54 
    55 < servlet-name >default</ servlet-name >
    56 
    57 < url-pattern >*.jsp</ url-pattern >
    58 
    59 </ servlet-mapping >
    60 
    61 < servlet-mapping >
    62 
    63 < servlet-name >default</ servlet-name >
    64 
    65 < url-pattern >*.html</ url-pattern >
    66 
    67 </ servlet-mapping >
    View Code

    spring文件配置。 有两个配置文件,一个是spring的,一个是spring MVC的。下面是spring的配置文件,指定扫描的包,并且扫描除了标注Controller注解之外的所有类。 Controller 这个注解是spring MVC用来标注一个类是否是web控制器。

    1 < context:component-scan   base-package = "me.riverslob.restBucks" >
    2 
    3 < context:exclude-filter   type = "annotation"   expression = "org.springframework.stereotype.Controller" />
    4 
    5 </ context:component-scan >
    View Code

    spring MVC的XML配置如下,使用<mvc:annotation-driven/>开启spring MVC的默认功能,随后会根据我们的需要来自定义。component-scan定义只扫描指定包的 标注 Controller注解的所有类。

    1 < mvc:annotation-driven />
    2 
    3 < context:component-scan   base-package = "me.riverslob.restBucks"   use-default-filters = "false" >
    4 
    5 < context:include-filter   type = "annotation"   expression = "org.springframework.stereotype.Controller" />
    6 
    7 </ context:component-scan >
    View Code

    三、Controller

    因为使用了注解,spring MVC控制器的定义也比较简单,下面是一个简单的例子。msgHandleService是底层提供的服务,可以看到spring MVC和spring整合的很好,我们用Autowired注解就框架就会自动帮我们注入进来。

    spring MVC中,每个方法都是一个单独的控制器,处理一个web请求。所以我们常常把一个资源的CRUD请求放在一个类中。

    在下面的例子中我们使用了这么几种注解:

    注解名称 注解范围 说明
    Controller 标识类会被spring MVC扫描管理
    RequestMapping 类或方法 标识请求路径,放在类上,表示通用前缀,方法上的value值会与类上的value值拼接成全访问路径。参数: 
    value:请求路径 
    method:限制当前url的请求方式,有GET、POST、PUT等
    RequestParam 方法入参 将request中的参数取出当作参数传入,可以处理简单类型,复杂类型可以自动转换
    PathVariable 方法入参 可以看到RequestMapping的value值里可以有通配符,我们可以用PathVariable注解把通配符里的值取出当作参数传入.这种使用方法大家称为URL模板

    在做web服务时候,下面这种用法全比较多。可以看到,入参是我们自定义类型,且前面多了 @RequestBody 注解,方法直接返回我们生成的数据,且前面多了 @ResponseBody 注解。这两个注解会经常用到,它 是框架自动帮我们做“java对象<--->JSON或XML格式数据”转换的标志 。随后我们会说到:message converter 

    1 @RequestMapping (method = RequestMethod.POST)
    2 
    3 public   @ResponseBody   WsUser create( @RequestBody   WsUser user) {
    4 
    5 WsUser user=wsUserService.create(user);
    6 
    7 return   user;
    8 
    9 }
    View Code

    在控制器中使用@ ResponseBody 或@ RequestBody,spring MVC就会自动帮我们 做“java对象<--->对应格式数据”的转换,用到的就是message converter。

    消息转换的最上层是HttpMessageConverter,这是一个接口,它定义了消息转换实现类需要提供的方法:

     

    根据方法名我们可以猜测其功能:canRead确定是否可以将数据转换为java对象,read则负责将数据转换为java对象;反之,canWrite确定是否可以将java对象转换为指定格式数据,write负责把java对象转换为对应格式的数据。这里有一个很重要的概念,就是MediaType,可以看到,messageConverter在做canWrite和canRead判断时,就是根据java对象类型和MediaType.

    MediaType: 互联网媒体类型,一般就是我们所说的 MIME 类型,用来确定请求的内容类型或响应的内容类型。

    注:Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 如果您的请求中含有 Accept:“*/*”,则可以匹配功能处理方法上的如“text/html”、“text/*”,“application/xml”等。

    媒体类型格式:type/subtype(;parameter)?

            type 主类型,任意的字符串,如 text,如果是*号代表所有;

            subtype 子类型,任意的字符串,如 html,如果是*号代表所有;

            parameter 可选,一些参数,如 Accept 请求头的 q 参数, Content-Type 的 charset 参数。

    详见 http://tools.ietf.org/html/rfc2616#section-3.7

    常见媒体类型:

    text/html : HTML 格式

        text/plain :纯文本格式 

        text/xml :XML 格式 

        image/gif :gif 图片格式 image/jpeg :jpg 图片格式 image/png:png 图片格式   

        application/x-www-form-urlencoded : <form encType=””>中默认的 encType,form 表单数据被编码为         key/value 格式发 送到服务器(表单默认的提交数据的格式)。

        multipart/form-data : 当你需要在表单中进行文件上传时,就需要使用该格式;

        application/xhtml+xml :XHTML 格式

        application/atom+xml :Atom XML 聚合格式

        application/pdf :pdf 格式

        application/octet-stream : 二进制流数据(如常见的文件下载)。

        application/xml: XML 数据格式

        application/json : JSON 数据格式

        可见,每个messageConverter会处理一个或几个MediaType到java对象的读、写转换。通过下面的类结构图我们就会一目了然。

    1. StringHttpMessageConverter

    它默认处理MediaType是text/plan和*/*(所有类型)的数据到java的转换。即String的转换处理,请注意,它默认的字符编码是ISO-8859-1,为避免乱码,我们改成UTF-8。spring bean的声明方式如下,也可以直接new 对象使用。因为它的构造函数中有默认编码参数,所以可以像下面这样设置。

    1 < bean   class = "org.springframework.http.converter.StringHttpMessageConverter" >
    2 
    3 < constructor-arg   name = "defaultCharset"   value = "UTF-8" />
    4 
    5 </ bean >
    View Code

    我们还可以调整它支持的MediaType,比如只支持text/plan:

     1 < bean   class = "org.springframework.http.converter.StringHttpMessageConverter" >
     2 
     3 < property   name = "supportedMediaTypes" >
     4 
     5 < list >
     6 
     7 < value >text/plan;charset=utf-8</ value >
     8 
     9 </ list >
    10 
    11 </ property >
    12 
    13 </ bean >
    View Code

    参考文献:

    http://blog.csdn.net/jbgtwang/article/details/7359592

  • 相关阅读:
    PHP页面跳转的几种方法
    PHP网站并发测试
    04-上传文件
    01-转>linux命令
    01-CDN的好处
    05-socket.io使用
    04-soket.io使用2 -数据同步简单聊天室效果
    03-socket.io 2.3.0版本的使用-用户请求接口,实时推送给前端数据
    02-转>
    跨域-转>预解析OPTIONS请求
  • 原文地址:https://www.cnblogs.com/dobestself-994395/p/4321135.html
Copyright © 2011-2022 走看看