spring mvc的mvc:annotation-driven以及日期的处理
来源:互联网 发布:音速启动是什么软件 编辑:程序博客网 时间:2024/04/30 08:52
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)
- spring mvc的mvc:annotation-driven以及日期的处理
- Spring中<mvc:annotation-driven />的作用
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring mvc拦截器和<mvc:annotation-driven />的详解
- Spring MVC 中,<mvc:annotation-driven /> 标签的注意点
- spring mvc拦截器和<mvc:annotation-driven />的详解
- spring-mvc拦截器和<mvc:annotation-driven />的详解
- Spring mvc:annotation-driven
- <mvc:annotation-driven/>的作用
- mvc:annotation-driven的使用
- spring怎么自动添加消息处理器的? <mvc:annotation-driven />
- java Applet绘图。。。
- 初观设计模式
- MySQL+Tomcat+J2EE中文乱码问题的UTF-8的解决方案
- hdu(欧拉函数应用,用筛法的方法求phi数组)
- Windows 8.1 Update 优缺点比较(找到使用一段时候后卡顿的原因了)
- spring mvc的mvc:annotation-driven以及日期的处理
- 【读书笔记】iOS6编程实战 17章国际化和本地化
- 多个文件目录下Makefile的写法
- ios遍历计算文件夹下文件总大小
- 空间闹钟-v1.6更新!
- JDK中设计模式
- 节点的增删改查
- instrumentation中AndroidTestCase学习笔记(5)
- JS计算网页停留时间代码分享