Spring Webflow DataBinding to immutable objects via a constructor?

﹥>﹥吖頭↗ 提交于 2019-12-18 13:34:36

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/7066405/spring-webflow-databinding-to-immutable-objects-via-a-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!