Springmvc框架-数据转换与格式化(一)
Springmvc框架数据绑定核心流程。
现在实现一个需求:
编写一个自定义的转换器 ,自定义转换规则,实现日期格式的转换。
在tools包下面创造StringToDateConvertor转化器类
1 package cn.smbms.tools; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 import org.springframework.core.convert.converter.Converter; 8 9 //创建一个自定义的时间日期格式转换器 10 public class StringToDateConvertor implements Converter<String, Date>{ 11 //创建一个构造器 12 private String datePattern; 13 public StringToDateConvertor(String dataPattern){ 14 this.datePattern=dataPattern;//dataPattern等于传递过来的dataPattern 15 } 16 //具体的转换方法 17 @Override 18 public Date convert(String time) { 19 Date date=null; 20 try { 21 date=new SimpleDateFormat(datePattern).parse(time); 22 } catch (ParseException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 return date; 27 } 28 29 }
Springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 15 <context:component-scan base-package="cn.smbms.controller"/> 16 <!-- 指定mvc:annotation-driven的默认实现,现在使用的是自己自定义的转化器 --> 17 <mvc:annotation-driven conversion-service="myConversionService"> 18 <mvc:message-converters> 19 <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 20 <property name="supportedMediaTypes"> 21 <list> 22 <value>application/json;charset=UTF-8</value> 23 </list> 24 </property> 25 </bean> 26 <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> 27 <property name="supportedMediaTypes"> 28 <list> 29 <value>text/html;charset=UTF-8</value> 30 <value>application/json</value> 31 </list> 32 </property> 33 <property name="features"> 34 <list> 35 <!-- Date的日期转换器 --> 36 <value>WriteDateUseDateFormat</value> 37 </list> 38 </property> 39 </bean> 40 </mvc:message-converters> 41 </mvc:annotation-driven> 42 <!--创建conversionService实例 当我们在遇到前后台日期类型转换的问题的时候,其实有mvc:annotation-driven一键式配置好了,所以conversionService实例不用也是可以的--> 43 <!-- <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"></bean> --> 44 45 <!-- 引用自己自定义的转化器 --> 46 <bean id="myConversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> 47 <property name="converters"> 48 <list> 49 <bean class="cn.smbms.tools.StringToDateConvertor"> 50 <!-- 构造注入 --> 51 <constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg> 52 53 </bean> 54 55 56 57 </list> 58 59 60 </property> 61 </bean> 62 63 <mvc:resources mapping="/statics/**" location="/statics/" /> 64 <!-- 完成视图的对应 --> 65 <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> 66 <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 67 <property name="prefix" value="/WEB-INF/jsp/"/> 68 <property name="suffix" value=".jsp"/> 69 </bean> --> 70 71 <!-- 配置多视图解析器:允许同样的内容数据呈现不同的view --> 72 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 73 <property name="favorParameter" value="true"/> 74 <property name="defaultContentType" value="text/html"/> 75 <property name="mediaTypes"> 76 <map> 77 <entry key="html" value="text/html;charset=UTF-8"/> 78 <entry key="json" value="application/json;charset=UTF-8"/> 79 <entry key="xml" value="application/xml;charset=UTF-8"/> 80 </map> 81 </property> 82 <!--配置网页的视图解析器 --> 83 <property name="viewResolvers"> 84 <list> 85 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 86 <property name="prefix" value="/WEB-INF/jsp/"/> 87 <property name="suffix" value=".jsp"/> 88 </bean> 89 </list> 90 </property> 91 </bean> 92 93 <!-- 全局异常处理 --> 94 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 95 <property name="exceptionMappings"> 96 <props> 97 <prop key="java.lang.RuntimeException">error</prop> 98 </props> 99 </property> 100 </bean> 101 102 <!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver --> 103 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 104 <property name="maxUploadSize" value="5000000"/> 105 <property name="defaultEncoding" value="UTF-8"/> 106 </bean> 107 108 </beans>
修改User.java类
运行结果: