SpringMVC请求参数转换
我可以使用 SpringMVC 中的属性编辑器对请求参数进行自定义转换。例如,要将请求参数转换为下面的 Foo
实例,
public class Foo {
private val;
public Foo(String val) {
this.val = val;
}
public getVal() {
return val;
}
}
我可以定义一个属性编辑器
public class FooPropertyEditor extends PropertyEditorSupport {
void setAsText(String paramValue) {
value = new Foo(paramValue);
}
public String getAsText() {
return ((Foo) value).getVal();
}
}
并注册它以执行从 String 到 Foo 的转换
public class CustomEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(Foo.class, new FooPropertyEditor());
}
}
是否可以使用属性编辑器来转换 multi -值参数,例如
foo=foo1&foo=foo2&foo=foo3
List
。假设我已经编写了一个适当的属性编辑器 FooListPropertyEditor
,我认为我无法使用以下方法注册它:
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(List<Foo>.class, new FooListPropertyEditor());
}
因为 AFAIK List
不是有效的语法
I can use property editors in SpringMVC to do custom conversion of a request parameter. For example to convert a request parameter to an instance of Foo
below
public class Foo {
private val;
public Foo(String val) {
this.val = val;
}
public getVal() {
return val;
}
}
I can define a property editor
public class FooPropertyEditor extends PropertyEditorSupport {
void setAsText(String paramValue) {
value = new Foo(paramValue);
}
public String getAsText() {
return ((Foo) value).getVal();
}
}
and register this to perform conversion from a String to a Foo
public class CustomEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(Foo.class, new FooPropertyEditor());
}
}
Is it possible to use a property editor to convert a multi-valued parameter such as
foo=foo1&foo=foo2&foo=foo3
to a List<Foo>
. Assuming I've already written an appropriate property editor FooListPropertyEditor
, I don't think I can register it using:
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(List<Foo>.class, new FooListPropertyEditor());
}
Because AFAIK List<Foo>.class
is not valid syntax
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看org.springframework.beans.propertyeditors.CustomCollectionEditor
take a look at org.springframework.beans.propertyeditors.CustomCollectionEditor