【发布时间】:2016-05-16 21:31:27
【问题描述】:
我正在使用 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 POST 时,控制器得到的是 null 而不是 [space][space]MyPassword
【问题讨论】:
-
这就是你配置的。
password是一个不允许的字段,因此它不会被绑定,因此它总是null。 -
我明白了。那我怎样才能实现我想要的呢?修剪用户在密码字段中输入的任何内容没有意义,但我确实希望它修剪其他字段,例如名称,其中空间更多是用户体验问题。
-
为密码字段注册一个特定的编辑器,而不是
StringTrimmerEditor。 -
问题是我如何排除密码字段的 StringTrimmerEditor ?我可以在 initBinder 中为 PasswordController 添加一个新的,但 ControllerAdvice 中的那个也会被添加
-
正如我所说,为密码字段注册一个a specific。这将优先于全局注册的。
标签: java spring spring-mvc model-view-controller trim