在Spring MVC中将表单值设置为NULL而不是空字符串 [英] Form values to be NULL instead of empty strings in Spring MVC

查看:560
本文介绍了在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?

预先感谢您的帮助.

推荐答案

您必须使用.您必须将此添加到您的控制器中.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆