绑定-数据类型转换
1.用法说明
2.JsonStringWriter
2.1.SojoJsonStringWriter
2.2.JsonlibJsonStringWriter
3.比较
1. 用法说明
Spring-Json View 当前提供了JSON-lib和SOJO的绑定支持。你可以从Spring Command或FormController中实现你已知的绑定方式。通常的做法是您在该控制器的initBinder方法内通过ServletRequestDataBinder注册一个CustomEditor。
默认情况下,Spring-MVC提供通过Command bean 的属性绑定CustomEditors:
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, editor); binder.registerCustomEditor(Date.class, "birthday", editor); }
2. JsonStringWriter
JsonStringWriter 管理model/Command对象间值的转换,当然,还有生成Json字符串。JsonStringWriter能最大限度地保持着您在Spring MVC所知道的CustomEditors的注册方式。在已知的Json类库的数据转换约定并不完全支持我们需要的特性。另一方面,有些Json类库的一些特定方式的特性你也可以使用。所以你可以按照JsonStringWriter的实现一些不同的绑定支持。
在下面的章节将告诉你它们将帮助你如何去选择合适的JsonStringWriter。
2.1 SojoJsonStringWriter - Sojo Json 支持
SojoJsonWriter 是Spring Json-View 的默认实现。它是一个通过Sojo Json 方式传输和非常接近默认Spring-MVC绑定行为的整合方案。
你可以绑定一个CustomEditors到:
可选:通过SojoJsonWriterConfiguratorTemplate可注册一个自定义“ SojoConfig ”对象。
2.2 JsonlibJsonStringWriter - Json-Lib支持
JsonlibJsonStringWriter 提供了Json-Lib框架到Spring Json-View的集成。我发现它没有按CommensBeanUtils-Syntax匹配ComandBean实现本地化属性的方法。但是你可以通过注册一个JsonlibJsonWriterConfiguratorTemplate从而完全操纵Json-Lib框架。它是一个"net.sf.json.JsonConfig"对象的封装。更多信息请参见Json-lib 主页。
你可以绑定一个CustomEditors到:
1. 像java.util.Date普通数据类型 (它显式转换成map model)
可选:通过JsonlibJsonWriterConfiguratorTemplate注册一个自定义"net.sf.json.JsonConfig" 对象。
3 比较
|
SojoJsonStringWriter |
JsonlibJsonStringWriter |
类库支持 |
SOJO-0.5.0 |
JSON-lib-2.2.1 |
默认写入器 |
是 |
否 |
ComandBean转换 |
|
|
转换类的类型 |
是 |
是 |
通过Commons BeanUtils语法定位属性 |
是 强类型Collection转换 |
否 |
其它Model Map属性转换 |
|
|
转换类的类型 |
可选 |
一直 |
通过Commons BeanUtils语法定位属性 |
可选 强类型Collection转换 |
否 |
JsonWriterConfiguratorTemplate |
可选 |
可选 |
SojoJsonWriter - Sojo Json 支持
1. 绪论
2. 简单绑定
3. 绑定CommandBean属性
4. 转换所有Model值
1. 通过CustomEditor转换所有值到Model-Map
2. 通过CustomEditor转换指定值到Model-Map
5. 注册SojoJsonWriterConfiguratorTemplates
1. 绪论
SojoJsonWriter 是Spring Json-View 的默认实现。它是一个通过Sojo Json 方式传输和接近默认Spring-MVC绑定方式的整合方案。
你可以绑定一个CustomEditors到:
注意:
Spring Json View 不能绑定如下例Bean中Collection类型中的属性:
Spring提供的标准写法:
bean.list.property
这个语法是定义conllection中从0-n的所有属性
Spring Json View则必须像下面这样使用解释型索引才能找出Collection-Beans的属性:
bean.list[0].property
bean.set[1].list[2].property
2. 简单绑定
initBinder 源文件:
=================
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, editor); }
效果:
=======
{"command":{
"birthday":"30-01-2008",
"marriage":"30-01-2008",
"placeofbirth":"Sydney"
}}
3. 绑定CommandBean属性
按CommonsBeanUtils语法定位CommandBean的属性
initBinder 源文件:
==================
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, "birthday", editor); }
效果:
======
{"command":{
"birthday":"30-01-2008",
"marriage":"Wed Jan 30 00:00:00 GMT 2008",
"placeofbirth":"Sydney"
}}
4. 转换所有Model的值
SojoJsonStringWriter可以为非CommandBean属性提供转换到Model Map。你可以通过在view.xml用JsonWriter- Bean设置convertAllMapValues 属性来激活这个特性。
您可以通过注册CustomEditor的开始部分的字段以定位它们(非commandbean键)。
* (name_in_model_map_key).property
* (key).list[1].property
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonWriter"/></property> </bean> <bean name="jsonWriter" class="org.springframework.web.servlet.view.json.writer.sojo.SojoJsonStringWriter"> <property name="convertAllMapValues"><value>true</value></property> </bean> </beans>
4.1 通过CustomEditor转换所有值到Model-Map
initBinder 源文件:
==================
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, editor); }
效果:
=======
{"signdate":"30-01-2008",
"command":{
"birthday":"30-01-2008",
"marriage":"30-01-2008",
"placeofbirth":"Sydney"
}}
4.2 通过CustomEditor转换指定值到Model-Map
initBinder 源文件:
==================
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, "birthday", editor); binder.registerCustomEditor(Date.class, "(signdate)", editor); }
效果:
=======
{"signdate":"30-01-2008",
"command":{
"birthday":"30-01-2008",
"marriage":"Wed Jan 30 00:00:00 GMT 2008",
"placeofbirth":"Sydney"
}}
5. 注册SojoJsonWriterConfiguratorTemplates
如果你想使用一个SojoJsonWriterConfiguratorTemplate,你必须要
推荐把SojoJsonWriterConfiguratorTemplate注册在initBinder方法中,但前提您应有任意一个可以发送请求的控制器方法。这个方法甚至可以是一个纯控制器接口实现的handleRequest 方法。
在spring配置文件中设置enableJsonConfigSupport属性
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonlibJsonWriter"/></property> </bean> <bean name="jsonlibJsonWriter" class="org.springframework.web.servlet.view.json.writer.jsonlib.JsonlibJsonStringWriter"> <property name="enableJsonConfigSupport"><value>true</value></property> </bean> </beans>
配置 : SojoConfig
SojoConfig 属性 | 函数 | 允许值 | 默认值 |
convertAllMapValues | 见 4. 转换所有Model值 | true, false | false |
ignoreNullValues | Model-Map中所有“null”值将被忽略其他如"属性":null将被自动转换为"null" | true, false | false |
excludedProperties | 整个Model Map的属性都能被排除在外 | 像(name_in_model_map_key )属性一样的字符串数组 commandbean属性 |
空 String - Array |
interceptorList | 增加注册Sojo WalkerInterceptor-Interface的实现,详见sojo主页 | Sojo WalkerInterceptor的实现 | 空 List |
注册SojoJsonWriterConfiguratorTemplates
initBinder 源码:
==================
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ JsonWriterConfiguratorTemplateRegistry registry = JsonWriterConfiguratorTemplateRegistry.load(request); registry.registerConfiguratorTemplate( new SojoJsonWriterConfiguratorTemplates(){ @Override public SojoConfig getJsonConfig() { SojoConfig config= new SojoConfig(); String[] excludes = new String[]{ "birthday" }; config.setExcludedProperties(excludes); return config; } } ); }
效果:
=======
{
"command":{
"placeofbirth":"Sydney"
}}
JsonlibJsonWriter - Json-Lib支持
1. 绪论
2. 使用
3. 绑定
4. 注册 JsonlibJsonWriterConfiguratorTemplates
1. 绪论
JsonlibJsonWriter的绑定能力相对较弱.
1.你只能绑定公共值的类类型到一个CustomEditor,如 java.util.Date到整个Model Map。您不能具体指定单个的bean属性或一个Collection的索引属性!
2. 你能通过注册JsonlibJsonWriterConfiguratorTemplate订制一个JsonConfig对象以完成"从model-map到JSON"的转换。
JsonlibJsonWriterConfiguratorTemplate 使你能够使用一些Json-Lib的高级特性,比如你能使用过滤或者触发一些事件。
注意:
JsonlibJsonWriter会 注册一个自己的JsonValueProcessor 并用JsonValueProcessorMatcher匹配它。
* JsonValueProcessor 使JsonlibJsonWriter 能够把CustomEditor注册到它的一个initBander方法里。
* JsonValueProcessorMatcher 会匹配JsonValueProcessor所有的值。JsonValueProcessor能决定这些值是否已经转换。
如果你想注册一个自定义的JsonValueProcessor或者 BeanProcessor,请记住,在某种程度上它并不能完全按你期望的可能来改变JsonlibJsonWriter 的行为,所以最好多测试你的程序。
* 您注册的任何一个自定义行为都不会影响“从请求属性到CommandBean”之间的绑定和转换。
2. 使用
注册一个JsonlibJsonStringWriter-Bean到JsonView。
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonlibJsonWriter"/></property> </bean> <bean name="jsonlibJsonWriter" class="org.springframework.web.servlet.view.json.writer.jsonlib.JsonlibJsonStringWriter"/> </beans>
3. 绑定
initBinder 源文件:
=================
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); CustomDateEditor editor = new CustomDateEditor(dateFormat, true); binder.registerCustomEditor(Date.class, editor); }
效果:
=======
{"signdate":"30-01-2008",
"command":{
"birthday":"30-01-2008",
"marriage":"30-01-2008",
"placeofbirth":"Sydney"
}}
4. 注册JsonlibJsonWriterConfiguratorTemplates
如果你想使用JsonlibJsonWriterConfiguratorTemplate,必须要
1. 在JsonlibJsonStringWriter设置"enableJsonConfigSupport"属性。
2. 实现JsonlibJsonWriterConfiguratorTemplate抽象类。
3. 注册JsonlibJsonWriterConfiguratorTemplate到JsonWriterConfiguratorTemplateRegistry。
推荐把JsonlibJsonWriterConfiguratorTemplate注册在initBinder方法中,但前提您应有任意一个可以发送请求的控制器方法。这个方法甚至可以是一个纯控制器接口实现的handleRequest 方法。
4.1
在Spring 配置文件里设置 "enableJsonConfigSupport"属性
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonWriter"><ref bean="jsonlibJsonWriter"/></property> </bean> <bean name="jsonlibJsonWriter" class="org.springframework.web.servlet.view.json.writer.jsonlib.JsonlibJsonStringWriter"> <property name="enableJsonConfigSupport"><value>true</value></property> </bean> </beans>
4.2 注册 JsonlibJsonWriterConfiguratorTemplate
initBinder 源文件:
==================
@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception{ JsonWriterConfiguratorTemplateRegistry registry = JsonWriterConfiguratorTemplateRegistry.load(request); registry.registerConfiguratorTemplate( new JsonlibJsonWriterConfiguratorTemplate(){ @Override public JsonConfig getJsonConfig() { JsonConfig config = new JsonConfig(); // Exclude all date properties config.setJsonPropertyFilter( new PropertyFilter(){ public boolean apply( Object source, String name, Object value ) { if( value != null && Date.class.isAssignableFrom( value.getClass() ) ){ return true; } return false; } }); return config; } } ); }
效果:
=======
{
"command":{
"placeofbirth":"Sydney"
}}