[英]InitBinder is not called/Invoked
我在JSP頁面上有單選按鈕。
<form:form modelAttribute="obj" action="save">
<form:radiobuttons path="person" items="${List}"/>
<form:button>Save</form:button>
</form:form>
列表:人員列表對象對象:
Class obj{
Person person;
getter/setters
}
在該JSP中,當選擇特定的單選按鈕時,我必須追加人員。
在控制器端
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String postProcess(@ModelAttribute("obj") Obj obj,BindingResult error) {
//processing
return "anotherJsp";
}
@InitBinder
public void initBinder(WebDataBinder binder, Locale locale, HttpServletRequest request) {
binder.registerCustomEditor(Obj.class,"person", new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
//processing for conversion of person object String to Person Object
setValue(text);
}
});
}
在這里我正在使用initbinder,因為我是從jsp獲取人員對象字符串的。所以,我正在獲取無法從String轉換為Person的綁定異常。
將數據綁定到modelAttribute之前,將調用InitBinder。因此,在initbinder中,我會將字符串轉換為Person對象。
此處的主要問題是My InitBinder在“未調用/已調用”中 。 請給我解決方案。
謝謝。
請通過放置斷點或調試語句來檢查是否正在調用它。
您的模型屬性名為“ obj”。 但是在屬性編輯器中,您已經使用“人”作為屬性路徑。
請嘗試將“ obj”用作propertyPath或刪除該參數並使用此簽名。 registerCustomEditor(Class requiredType,PropertyEditor propertyEditor類)
希望能幫助到你。
黑暗中的狂野刺殺,是否從org.springframework.web.bind.annotation
導入正確的@InitBinder
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.