[英]Is there a way to map a Spring 3.0 MVC @RequestParam directly to a Java Bean?
Can this somehow work? 这可以以某种方式工作吗? do I have to use
@InitBinder
somehow? 我必须以某种方式使用
@InitBinder
吗?
public String myActionHandler(ModelMap model, @RequestParam MyPojoBean myBean){
...
}
I'm sure I've seen this somewhere, but I'm not sure where. 我敢肯定我在某个地方见过,但是我不确定在哪里。 Is there a simple code example for this?
是否有一个简单的代码示例?
If the above is possible, how can I catch the exception if the request doesen't match the Bean? 如果以上情况可行,如果请求与Bean不匹配,如何捕获异常?
You need to register a custom editor in initBinder
: 您需要在
initBinder
注册一个自定义编辑器:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(MyPojoBean.class, new MyPojoEditor());
}
class MyPojoEditor extends java.beans.PropertyEditorSupport {
@Override public String getAsText () {...}
@Override public void setAsText (String s) {...}
}
您可以使用@InitBinder
(请参见@InitBinder
的答案)来执行此操作,如果只需要针对单个类执行此操作,或者使用更通用的自定义WebArgumentResolver
(请参见此其他问题 ),则最好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.