我厌倦了为系统中的每个实体编写扩展 PropertyEditorSupport。
我注意到 Spring 3.0 有一个 IdToEntityConverter,但是关于如何使用它的文档真的不多。
请评论在 id 和实体之间转换的最佳通用方法。
最佳答案
IdToEntityConverter
是一个内部 Spring 类。它是非公开的,这就是它没有出现在 javadoc 中的原因,但它在每个上下文中都默认注册。类评论说:
Converts an entity identifier to a entity reference by calling a static finder method on the target entity type.
For this converter to match, the finder method must be public, static, have the signature
find[EntityName]([IdType])
, and return an instance of the desired entity type.
因此,如果您尝试绑定(bind)实体类 X
,则 X
必须具有 public static findX(id)
或其他内容类似的,在 X
类上。
关于java - 如何在表单绑定(bind)期间使用 Spring 的 IdToEntityConverter 进行实体类型转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2324034/