gpt4 book ai didi

深入理解Spring MVC的数据转换

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章深入理解Spring MVC的数据转换由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文主要给大家介绍了关于Spring MVC数据转换的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧.

数据绑定 。

SpringMVC负责将request中的信息以一定的方式转换并绑定到处理方法的参数上。整个过程的处理核心是由DataBinder完成。转换流程如下:

     1.DataBinder从ServletRequest中获取参数信息; 。

     2.DataBinder获取处理方法的参数; 。

     3.DataBinder调用ConversionService组件数据类型转换和数据格式化工作,并将转化结果填充到参数对象中; 。

     4.DataBinder调用Validator组件进行数据的校验工作; 。

     5.经历以上步骤后,DataBinder将生成BinderResult对象,BinderResult中包含转换后的信息,也包含校验后的错误信息.

数据转换 。

在java语言中,在java.beans包中提供了一个PropertyEditor接口来进行数据转换,PropertyEditor的核心功能是将一个String转换为一个java对象。Spring从3.0开始添加一个通用的类型转换模块即为org.springframework.convert包中,ConversionService是org.springframework.convert包的核心组件,可以通过使用ConversionServiceFactoryBean在spring的上下文中自定义一个ConversionService,Spring将自动识别这个ConversionService,并在SpringMVC进行参数转换时使用,配置例子如下所示:

?
1
2
3
4
5
6
7
8
<bean id= "conversionService"
  class = "org.springframework.context.support.ConversionServiceFactoryBean" >
  <property name= "converters" >
   <list>
   <bean class = "org.xx..StringToDateConverter" />
   </list>
  </property>
</bean>

SpringMVC在支持新的转换器框架的同时,也支持javabeans的PropertyEditor,可以在控制器中使用@InitBinder添加自定义的编辑器.

举例如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Controller
public class DataBinderTestController {
  @RequestMapping (value = "/dataBind" )
  public String test(DataBinderTestModel command) {
  ......
  }
  @InitBinder
 
  public void iniiBinder(WebDataBinder binder){
  
  SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" );
  format.setLenient( false );
  binder.registerCustomEditor(Date. class , new CustomDateEditor(format, false ));
  }
}

各种转换器的优先顺序:

      1.查询通过@InitBinder自定义的编辑器; 。

      2.查询通过ConversionService装配的自定义转换器; 。

      3.查询通过WebBindingInitializer接口装配的全局自定义编辑器.

Formater 。

除了org.springframework.core.convert.converter接口中定义的三种类型的转换器接口,SpringMVC在org.springframework.format包中还提供了一些格式化转换接口,format和converter的最大的区别是,converter实现的是object到object的转换,而format实现的是从String到Object的转换,format包中最重要的接口是Formater,Formater的使用示例如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DateFormatter extends Formatter<Date>{
  private String datePattern;
 
  private SimpleDateFormat dateFormat;
 
  public DateFormatter(String datePattern){
  this .datePattern=datePattern;
  this .dateFormat= new SimpleDateFormat(datePattern);
  }
 
  public String pring(Date,Locale locale){
  return dateFormat.format(date);
  }
 
  public Date parse(String source,Locale locale) throws ParseException{
  try {
   return dateFormat.parse(source);
  } catch (Exception e){
   ......
  }
  }
}

最后再将DateFormatter注入到ConversionService中,注入方式和Converter的注入方式一样,也可由此发现,ConversionService是数据转换的核心.

Format的注解 。

在org.springframework.format.annotation包中定义了两个注解,@DateTimeFormat和@NumberFormat 这两个注解可以用在domain中的属性上,SpringMVC处理方法参数绑定数据、模型数据输出时会自动通过注解应用格式化的功能.

总结 。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.

原文链接:https://segmentfault.com/a/1190000011340970 。

最后此篇关于深入理解Spring MVC的数据转换的文章就讲到这里了,如果你想了解更多关于深入理解Spring MVC的数据转换的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com