通过构造函数将Spring Webflow DataBinding绑定到不可变对象吗? [英] Spring Webflow DataBinding to immutable objects via a constructor?

查看:152
本文介绍了通过构造函数将Spring Webflow DataBinding绑定到不可变对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring Webflow的视图状态内,有什么方法可以将不可变对象用作模型?我知道Spring Webflow通常倾向于这种设置方法,但是我想知道是否有人知道可以使用构造函数来处理数据绑定的自定义DataBinder或WebDataBinder?

我还知道,针对该任务的 https://jira.springsource.org/browse/SPR-1488"rel =" noreferrer>(SPR-1488) JIRA任务问题,提倡直接现场访问.人们提倡这种做事方式吗?对我来说,感觉并不对.

谢谢

斯图尔特

解决方案

我举了一个示例,说明如何使用Jackson的ObjectMapper(除了JSON以外,还可以很好地将任何内容映射到不可变的对象)来实现此目的.

https://gist.github.com/4458079

需要注意的几件事是,如果要使用Spring的验证(BindingResult),则不能使用@ModelAttribute,除非您替换了所有参数解析器.但是@Valid(具有验证)应该可以,并且@RequestBody也可以(未经验证)与我的解决方案一起使用.

尽管它使用Jackson来将请求参数映射到对象,但不涉及JSON处理.如果需要,请参见不可变的ORM称为JIRM 结合使用消息驱动的体系结构的不可变对象.

Is there any way of using an immutable object as a model within a view-state in Spring webflow? I know Spring webflow generally tends towards setters for this kind of thing, but I was wondering if anyone knew of a custom DataBinder or WebDataBinder that could handle binding the data using a constructor?

I'm also aware there's this (SPR-1488) JIRA task against the problem, which advocated direct field access. Do people advocate this way of doing things? To me it doesn't quite feel right.

Thanks,

Stuart

解决方案

I have put an example of how you can do this using Jackson's ObjectMapper (which besides JSON does a good job mapping anything to immutable objects ).

https://gist.github.com/4458079

A couple of things to note is that you can't use @ModelAttribute if you want to use Spring's validation (BindingResult) unless you replace all the argument resolvers. However @Valid (with validation) should work and @RequestBody will also work (with out validation) with my solution.

Although its using Jackson to map request parameters to objects there is no JSON processing involved. If you want that see MappingJacksonHttpMessageConverter.

As a feeble shameless plug I needed this in conjunction with my Immutable ORM called: JIRM as I prefer immutable objects for message driven architectures.

这篇关于通过构造函数将Spring Webflow DataBinding绑定到不可变对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆