简体   繁体   中英

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?

public String myActionHandler(ModelMap model, @RequestParam MyPojoBean myBean){
    ...
}
  1. I'm sure I've seen this somewhere, but I'm not sure where. Is there a simple code example for this?

  2. If the above is possible, how can I catch the exception if the request doesen't match the Bean?

You need to register a custom editor in 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 (请参见此其他问题 ),则最好。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM