我正在使用 Spring MVC。我的页面上有一个表格。当我没有在表单中输入任何值并提交时,它带有空字符串 ""
.
是否可以设置 null
值而不是空字符串?
提前感谢您的帮助。
最佳答案
您必须使用 @InitBinder注释,因为在 Spring MVC 中,它总是为表单中的空白值返回 ""
。您必须在 Controller 中添加它。
例子:
@InitBinder /* Converts empty strings into null when a form is submitted */
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
关于forms - 在 Spring MVC 中将值设置为 NULL 而不是空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647214/