使用 Spring MVC
事实证明,我可以通过多种不同的方式绑定我的表单,但我真的感觉自己迷失了方向。 Formatter
的 parse
和 print
方法相当于具有不同名称的 PropertyEditorSupport
的方法( >getAsText
和 setAsText
)。同样,我可以实现一个 GenericConverter
或两个 Converter
来完成完全相同的操作。
我在此处的评论中读到Formatters
是 PropertyEditor
的替代品,但我还没有找到任何支持它的文档,而且它甚至还没有被弃用。
我的问题是,当涉及到将数据从表单绑定到对象时,在 spring-mvc 中正确的方法是什么? Spring中的PropertyEditor
、Formatter
和Converter
之间的主要区别是什么?每一种的用例是什么?对我来说,他们似乎负有同样的责任。
Working with Spring MVC
it turns out that I can bind my forms in many different ways and I really feel like I'm getting lost. The parse
and print
methods of a Formatter
are equivalent to those of a PropertyEditorSupport
with a different name (getAsText
and setAsText
). Similarly, I can implement either a GenericConverter
or two Converter<S,T>
s to do exactly the same thing.
I read here in a comment that Formatters
are a replacement for PropertyEditor
, but I haven't found any documentation to support it and it hasn't even been Deprecated neither.
My question is, when it comes to bind data from a form to an object, what is the correct way to do it in spring-mvc
? What is the main difference between PropertyEditor
, Formatter
and Converter
in Spring? What are the use cases for each one? For me it looks like they have the same responsibility.
发布评论
评论(1)
为了帮助理解这些概念,我首先将 Spring 特定功能与 Java 公开的功能区分开来。
PropertyEditor
和 相关内容由JavaBeans 规范定义。该规范定义了用于处理对象、对象属性以及与其更改相关的所有内容(作为事件)的 API、机制和约定。
PropertyEditor
通常在 GUI 中用于处理 UI 和底层对象模型之间的交互,通常处理属性值与其String
表示形式之间的转换。Spring 本身实际上在许多不同的情况下使用不同的
PropertyEditor
实现和 Java Beans 约定。例如,来自 docs< /a>:总之,PropertyEditor 允许您实现更广泛的用例。
现在,在 Spring 世界中,您还需要区分 Spring MVC 和 Spring Core。
请注意,转换 和 Formatter 的东西是被定义为核心技术,与任何用例相关,但不限于网络框架。
Spring 文档,在描述 Spring 字段格式,对每个 API/SPI 的用途以及它们与
PropertyEditor
的关系提供了很好的解释:在 Spring MVC 的特定用例中,框架本身能够处理简单类型,当 处理 HTTP 请求。
类型转换是根据配置的转换器集自动应用,尽管可以使用
DataBinder
s 和前面提到的格式化系统。请参阅相关文档< /a>.在处理读取和写入 HTTP 请求和响应正文的典型用例中,当使用
@RequestBody
,例如Spring会使用一堆不同的预已配置HttpMessageConverter
< /a> 实现:实际注册的库将取决于您的配置和项目中导入的库 - 例如杰克逊。我无法在文档中找到这一点,但这里是实际 源代码。请考虑查看这个相关的SO问题,它可能是的帮助。
To help to understand these concepts I would differentiate first the Spring specific functionality from that exposed from Java.
PropertyEditor
s and the related stuff are defined by the JavaBeans Specification.The specification defines an API, mechanisms and conventions for dealing with objects, objects properties, and everything related to their changes, as events.
PropertyEditor
s are typically used in GUIs to handle the interaction between an UI and the underlying objects model, typically handling the conversion between properties values from/to itsString
representation.Spring itself actually uses different
PropertyEditor
implementations and Java Beans conventions in many different situations. For example, from the docs:In summary,
PropertyEditor
s allow you for a broader number of use cases.Now, in the Spring world you need to do a differentiation as well between Spring MVC and Spring Core.
Please, note that both the Convert and the Formatter stuff are defined as core technologies, of relevance to any use case and not limited to the web framework.
The Spring documentation, when describing Spring Field Formatting, provides a great explanation about the purpose of every API/SPI and how they are related to
PropertyEditor
s as well:In the specific use case of Spring MVC the framework itself is able to handle simple types when handling the HTTP requests.
Type conversion is automatically applied based on the configured set of converters, although that behavior can be tweaked using
DataBinder
s and the aforementioned Formatting system. Please, see the relevant docs.In a typical use case in which you deal with reading and writing the body of HTTP requests and responses, when using the
@RequestBody
, for example, Spring will use a bunch of different pre-configuredHttpMessageConverter
implementations: the actual ones registered will depend on your configuration and the libraries imported in your project - say Jackson, for example. I was unable to find that point in the documentation but here is the link to the actual source code.Please, consider review this related SO question, it could be of help.