表单值在 Spring MVC 中为 NULL 而不是空字符串 [英] Form values to be NULL instead of empty strings in Spring MVC
本文介绍了表单值在 Spring MVC 中为 NULL 而不是空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Spring MVC.我的页面上有一个表格.当我没有在表单中输入任何值并提交时,它带有空字符串 ""
.
I am using Spring MVC. I have a form on my page. When I do not enter any values to the form and submit it, it comes with empty strings ""
.
是否可以设置 null
值而不是空字符串?
Is it possible to set null
value instead of empty strings?
预先感谢您的帮助.
推荐答案
你必须使用 @InitBinder 注释,因为在 Spring MVC 中它总是返回 ""
中的空白值表单.您必须将其添加到您的控制器中.
You have to use the @InitBinder annotation because in Spring MVC it always returns ""
for the blank values in a form. You have to add this in your controller.
示例:
@InitBinder /* Converts empty strings into null when a form is submitted */
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
这篇关于表单值在 Spring MVC 中为 NULL 而不是空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文