使用 Spring MVC
事实证明,我可以用许多不同的方式绑定(bind)我的表格,我真的觉得自己迷路了。 parse
和 print
Formatter
的方法相当于那些 PropertyEditorSupport
使用不同的名称( getAsText
和 setAsText
)。同样,我可以实现 GenericConverter
或两个 Converter<S,T>
让我们做同样的事情。
我读了here在评论中 Formatters
是 PropertyEditor
的替代品,但我还没有找到任何支持它的文档,它甚至都没有被弃用。
我的问题是,当涉及到将数据从表单绑定(bind)到对象时,在 spring-mvc
中执行此操作的正确方法是什么? ? PropertyEditor
之间的主要区别是什么? , Formatter
和 Converter
在 Spring ?每个的用例是什么?对我来说,他们似乎负有同样的责任。
最佳答案
为了帮助理解这些概念,我首先要区分 Spring 的特定功能与 Java 公开的功能。
PropertyEditor
和 the related stuff由 JavaBeans Specification 定义.
该规范定义了一个 API、机制和约定,用于处理对象、对象属性以及与其更改相关的所有内容,如事件。
PropertyEditor
通常在 GUI 中用于处理 UI 和底层对象模型之间的交互,通常处理属性值与其 String
表示之间的转换.
Spring 本身实际上在许多不同的情况下使用不同的 PropertyEditor
实现和 Java Beans 约定。例如,来自 docs :
A couple of examples where property editing is used in Spring:
Setting properties on beans is done by using
PropertyEditor
implementations. When you use String as the value of a property of some bean that you declare in an XML file, Spring (if the setter of the corresponding property has aClass
parameter) usesClassEditor
to try to resolve the parameter to aClass
object.Parsing HTTP request parameters in Spring’s MVC framework is done by using all kinds of
PropertyEditor
implementations that you can manually bind in all subclasses of theCommandController
.
总而言之,PropertyEditor
允许您使用更多的案例。
现在,在 Spring 世界中,您还需要区分 Spring MVC 和 Spring Core。
请注意 Convert和 Formatter东西被定义为核心技术,与任何用例相关,不限于网络框架。
Spring 文档,在描述时 Spring Field Formatting , 提供了关于每个 API/SPI 的目的以及它们如何与 PropertyEditor
相关的很好的解释:
As discussed in the previous section,
core.convert
is a general-purpose type conversion system. It provides a unifiedConversionService
API as well as a strongly typed Converter SPI for implementing conversion logic from one type to another. A Spring container uses this system to bind bean property values. In addition, both the Spring Expression Language (SpEL) andDataBinder
use this system to bind field values. For example, when SpEL needs to coerce aShort
to aLong
to complete anexpression.setValue(Object bean, Object value)
attempt, thecore.convert
system performs the coercion.Now consider the type conversion requirements of a typical client environment, such as a web or desktop application. In such environments, you typically convert from
String
to support the client postback process, as well as back toString
to support the view rendering process. In addition, you often need to localizeString
values. The more general core.convert Converter SPI does not address such formatting requirements directly. To directly address them, Spring 3 introduced a convenient Formatter SPI that provides a simple and robust alternative toPropertyEditor
implementations for client environments.In general, you can use the Converter SPI when you need to implement general-purpose type conversion logic — for example, for converting between a
java.util.Date
and aLong
. You can use the Formatter SPI when you work in a client environment (such as a web application) and need to parse and print localized field values. TheConversionService
provides a unified type conversion API for both SPIs.
在 Spring MVC 的特定用例中,框架本身能够在 handling the HTTP requests 时处理简单类型.
Type conversion根据配置的转换器集自动应用,尽管可以使用 DataBinder
s 调整该行为和前面提到的格式化系统。请参阅 relevant docs .
在处理读取和写入 HTTP 请求和响应正文的典型用例中,当使用 @RequestBody
时,例如,Spring 会使用一堆不同的预 configured HttpMessageConverter
实现:实际注册的将取决于您的配置和项目中导入的库 - 例如 Jackson。我无法在文档中找到这一点,但这里是实际 source code 的链接.
请考虑审查 this related SO question , 它可能会有所帮助。
关于java - Spring 中的 PropertyEditor、Formatter 和 Converter 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71515687/