我正在使用Spring的registerCustomEditor
修剪所有前导/尾随空格,但我不希望它在特定字段上执行,例如密码字段。
@ControllerAdvice
public class MyCustomControllerAdvice {
@InitBinder
public void setupDefaultInitBinder(WebDataBinder binder) {
binder.setDisallowedFields("*password");
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
}
现在,当我使用password=[space][space]MyPassword
发布时,控制器正在获取null
而不是[space][space]MyPassword
答案 0 :(得分:0)
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
PropertyEditorSupport dummyEditor = new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
super.setValue(text);
}
};
binder.registerCustomEditor(String.class, "password", dummyEditor);
}