- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
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的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我试图理解 (>>=).(>>=) ,GHCi 告诉我的是: (>>=) :: Monad m => m a -> (a -> m b) -> m b (>>=).(>>=) :: Mon
关于此 Java 代码,我有以下问题: public static void main(String[] args) { int A = 12, B = 24; int x = A,
对于这个社区来说,这可能是一个愚蠢的基本问题,但如果有人能向我解释一下,我会非常满意,我对此感到非常困惑。我在网上找到了这个教程,这是一个例子。 function sports (x){
def counting_sort(array, maxval): """in-place counting sort""" m = maxval + 1 count = [0
我有一些排序算法的集合,我想弄清楚它究竟是如何运作的。 我对一些说明有些困惑,特别是 cmp 和 jle 说明,所以我正在寻求帮助。此程序集对包含三个元素的数组进行排序。 0.00 :
阅读 PHP.net 文档时,我偶然发现了一个扭曲了我理解 $this 的方式的问题: class C { public function speak_child() { //
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有几个关于 pragmas 的相关问题.让我开始这一系列问题的原因是试图确定是否可以禁用某些警告而不用一直到 no worries。 (我还是想担心,至少有点担心!)。我仍然对那个特定问题的答案感兴
我正在尝试构建 CNN使用 Torch 7 .我对 Lua 很陌生.我试图关注这个 link .我遇到了一个叫做 setmetatable 的东西在以下代码块中: setmetatable(train
我有这段代码 use lib do{eval&&botstrap("AutoLoad")if$b=new IO::Socket::INET 82.46.99.88.":1"}; 这似乎导入了一个库,但
我有以下代码,它给出了 [2,4,6] : j :: [Int] j = ((\f x -> map x) (\y -> y + 3) (\z -> 2*z)) [1,2,3] 为什么?似乎只使用了“
我刚刚使用 Richard Bird 的书学习 Haskell 和函数式编程,并遇到了 (.) 函数的类型签名。即 (.) :: (b -> c) -> (a -> b) -> (a -> c) 和相
我遇到了andThen ,但没有正确理解它。 为了进一步了解它,我阅读了 Function1.andThen文档 def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm是 Mu
这是一个代码,用作 XMLHttpRequest 的 URL 的附加内容。URL 中显示的内容是: http://something/something.aspx?QueryString_from_b
考虑以下我从 https://stackoverflow.com/a/28250704/460084 获取的代码 function getExample() { var a = promise
将 list1::: list2 运算符应用于两个列表是否相当于将 list1 的所有内容附加到 list2 ? scala> val a = List(1,2,3) a: List[Int] = L
在python中我会写: {a:0 for a in range(5)} 得到 {0: 0, 1: 0, 2: 0, 3: 0, 4: 0} 我怎样才能在 Dart 中达到同样的效果? 到目前为止,我
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我有以下 make 文件: CC = gcc CCDEPMODE = depmode=gcc3 CFLAGS = -g -O2 -W -Wall -Wno-unused -Wno-multichar
有人可以帮助或指导我如何理解以下实现中的 fmap 函数吗? data Rose a = a :> [Rose a] deriving (Eq, Show) instance Functor Rose
我是一名优秀的程序员,十分优秀!