问题描述
我正在寻找在 Spring MVC 中绑定和转换数据的最简单和最简单的方法.如果可能,不做任何 xml 配置.
I am looking for the easiest and simplest way to bind and convert data in Spring MVC. If possible, without doing any xml configuration.
到目前为止我一直在使用 PropertyEditors 像这样:
So far I've been using PropertyEditors like so :
public class CategoryEditor extends PropertyEditorSupport {
// Converts a String to a Category (when submitting form)
@Override
public void setAsText(String text) {
Category c = new Category(text);
this.setValue(c);
}
// Converts a Category to a String (when displaying form)
@Override
public String getAsText() {
Category c = (Category) this.getValue();
return c.getName();
}
}
和
...
public class MyController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Category.class, new CategoryEditor());
}
...
}
很简单:两个转换都定义在同一个类中,绑定很简单.如果我想对所有控制器进行通用绑定,我仍然可以添加 我的 xml 配置中的 3 行.
It is simple : both conversion are defined in the same class, and the binding is straightforward. If I wanted to do a general binding across all my controllers, I could still add 3 lines in my xml config.
但是 Spring 3.x 引入了一种新方法,使用 转换器 :
But Spring 3.x introduced a new way to do it, using Converters :
在一个 Spring 容器中,这个系统可以作为一个替代方案给属性编辑器
所以假设我想使用转换器,因为它是最新的替代方案".我必须创建两个转换器:
So let's say I want to use Converters because it is "the latest alternative". I would have to create two converters :
public class StringToCategory implements Converter<String, Category> {
@Override
public Category convert(String source) {
Category c = new Category(source);
return c;
}
}
public class CategoryToString implements Converter<Category, String> {
@Override
public String convert(Category source) {
return source.getName();
}
}
第一个缺点:我必须创建两个类.好处:由于通用性,无需强制转换.
First drawback : I have to make two classes. Benefit : no need to cast thanks to genericity.
那么,我如何简单地对转换器进行数据绑定?
Then, how do I simply data bind the converters ?
第二个缺点:我还没有找到任何简单的方法(注释或其他编程工具)在控制器中做到这一点:没有像 someSpringObject.registerCustomConverter(...);代码>.
Second drawback : I haven't found any simple way (annotations or other programmatic facilities) to do it in a controller : nothing like someSpringObject.registerCustomConverter(...);
.
我发现的唯一方法是乏味的,不简单的,而且只是关于通用的跨控制器绑定:
The only ways I've found would be tedious, not simple, and only about general cross-controller binding :
XML 配置 :
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="somepackage.StringToCategory"/>
<bean class="somepackage.CategoryToString"/>
</set>
</property>
</bean>
Java 配置(仅在 Spring 3.1+ 中):
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
protected void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToCategory());
registry.addConverter(new CategoryToString());
}
}
有所有这些缺点,为什么要使用转换器?我错过了什么吗?还有其他我不知道的技巧吗?
With all these drawbacks, why using Converters ? Am I missing something ? Are there other tricks that I am not aware of ?
我很想继续使用 PropertyEditors...绑定更容易、更快捷.
I am tempted to go on using PropertyEditors... Binding is much easier and quicker.
推荐答案
不,我认为您已经非常全面地描述了 PropertyEditor 和 Converter,它们是如何声明和注册的.
No, I think you have very comprehensively described both PropertyEditor and Converter, how each one is declared and registered.
在我看来,PropertyEditor 的范围是有限的——它们帮助将 String 转换为类型,并且这个字符串通常来自 UI,因此使用 @InitBinder 和使用 WebDataBinder 注册一个 PropertyEditor 是有意义的.
In my mind, PropertyEditors are limited in scope - they help convert String to a type, and this string typically comes from UI, and so registering a PropertyEditor using @InitBinder and using WebDataBinder makes sense.
另一方面,Converter 更通用,它用于系统中的任何转换——不仅仅是 UI 相关的转换(字符串到目标类型).例如,Spring Integration 广泛使用转换器将消息有效负载转换为所需类型.
Converter on the other hand is more generic, it is intended for ANY conversion in the system - not just for UI related conversions(String to target type). For eg, Spring Integration uses a converter extensively for converting a message payload to a desired type.
我认为对于 UI 相关的流程,PropertyEditor 仍然适用,尤其是在您需要为特定命令属性做一些自定义的情况下.对于其他情况,我会接受 Spring 参考中的建议并编写一个转换器(例如,将 Long id 转换为实体,例如作为示例).
I think for UI related flows PropertyEditors are still appropriate especially for the case where you need to do something custom for a specific command property. For other cases, I would take the recommendation from Spring reference and write a converter instead(for eg, to convert from a Long id to an entity say, as a sample).
这篇关于Spring MVC 类型转换:PropertyEditor 还是 Converter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!