我正在使用 spring,并且我有一个带有 objectify 关键对象的子模型 - “Key parent”
加载表单时 getAsText 打印正常,但是当表单提交时, setAsText 被跳过。任何原因?当它到达控制器时 parentType 是空的。是表单问题还是控制器还是编辑器?
(旁道:是否有为 Objectify Key <-> String 映射编写的编辑器?)
jsp
<form:hidden path="parentType" />
控制器
@InitBinder
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
/** Key Conversion **/
binder.registerCustomEditor(com.googlecode.objectify.Key.class, new KeyEditor());
}
@RequestMapping(value = "/subtype-formsubmit", method = RequestMethod.POST)
public ModelAndView subTypeFormSubmit(@ModelAttribute("productSubType") @Valid ProductSubType productSubType,
BindingResult result){
ModelAndView mav = new ModelAndView();
//OK, getting the value
log.info(productSubType.getType());
//NOT OK, productSubType.getParentType() always null, and setAsText is not called?!
log.info(productSubType.getParentType().toString());
return mav;
}
KeyEditor.java
public class KeyEditor extends PropertyEditorSupport {
private static final Logger log = Logger
.getLogger(KeyEditor.class.getName());
public KeyEditor(){
super();
}
@Override
public String getAsText() {
Long id = ((Key) getValue()).getId();
String kind = ((Key) getValue()).getKind();
log.info(kind + "." + id.toString());
return kind + "." + id.toString();
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
log.info(text);
String clazz = text.substring(0, text.indexOf("."));
Long id = Long.parseLong(text.substring(text.indexOf(".")));
log.info(clazz+":"+id);
Class c=null;
Key<?> key=null;
try {
c = Class.forName(clazz);
key = new Key(c, id);
} catch (Exception ex) {
log.info("ex" + ex.toString());
ex.printStackTrace();
}
setValue(key);
}
}