我是靠谱客的博主 善良绿茶,最近开发中收集的这篇文章主要介绍spring mvc的mvc:annotation-driven以及日期的处理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

<mvc:annotation-driven />是什么意思?参考手册http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/pdf/spring-framework-reference.pdf会讲得比较清楚
17.15 Configuring Spring MVC讲到<mvc:annotation-driven />就是注册了一个RequestMappingHandlerMapping,一个RequestMappingHandlerAdapter和一个ExceptionHandlerExceptionResolver(其中包括)支持使用注解标注在Controller方法的处理请求,例如@RequestMapping ,@ExceptionHandler等等
它还执行以下操作:
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.
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


如果不使用mvc:annotation-driven,日期又如何处理.

spring mvc默认是支持yyyy-MM-dd格式的字符串转换为java的java.util.Date.包括spring mvc框架本身和spring mvc支持的jackson.
对于其它格式的日期的字符串与Java的Date对象相互转化,一样可分为两种情况:
A:一种普通请求,前台的日期字符串与后台的Java Date对象转化,此情况,应使用spring mvc本身的内置日期处理.
B:另一种将参数写进请求体里面,使用application/json这样的mediaType发请求,对于此情况,应使用Jackson的序列化和反序列化来处理.

一.第1种情况(不要忘了加joda-time包哦):
1.先使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")在Controller的方法参数或VO的属性使用.
2.如果不使用mvc:annotation-driven,那么使用数据绑定来处理@DateTimeFormat这样的注解.配置例子如下:

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="webBindingInitializer">
            <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <property name="conversionService" ref="conversionService" />
            </bean>
        </property>
        <property name="messageConverters">
            <list>
                <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json; charset=UTF-8</value>
                            <value>text/html; charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <bean id="conversionService" class="org.springframework.format.support.DefaultFormattingConversionService"/>
二.第2种情况:
1.继承定义序列化和反序列化类.例子:
public class DateJsonSerializer extends JsonSerializer<Date> {
    public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeString(format.format(date));
    }
}
public class DateJsonDeserializer extends JsonDeserializer<Date> {
    public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        try {
            return format.parse(jsonParser.getText());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
2.在VO使用@JsonSerialize(using = DateJsonSerializer.class)和@JsonDeserialize(using = DateJsonDeserializer.class)注解(属性或方法都可以,序列化标注在get方法,反序列化标注在set方法).

@JsonSerialize(using = DateJsonSerializer.class)

@JsonDeserialize(using = DateJsonDeserializer.class)

private Date createTime;

最后

以上就是善良绿茶为你收集整理的spring mvc的mvc:annotation-driven以及日期的处理的全部内容,希望文章能够帮你解决spring mvc的mvc:annotation-driven以及日期的处理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部