java 有没有一种简单的方法可以在 Spring MVC 中将空表单输入转换为空字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2977649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:47:22  来源:igfitidea点击:

Is there an easy way to turn empty form inputs into null strings in Spring MVC?

javaspringspring-mvcform-submit

提问by Brett Stottlemyer

I'm using Spring MVC and SimpleJdbcInsertto insert objects into a MySQL database. I'd like to set the blank input to NULLin the database rather than ''. I have quite a few fields, and I'm hoping for a way to do this without manually checking every value.

我正在使用 Spring MVC 和SimpleJdbcInsert将对象插入 MySQL 数据库。我想NULL在数据库中设置空白输入而不是''. 我有很多字段,我希望有一种方法可以在不手动检查每个值的情况下做到这一点。

Thanks!

谢谢!



UPDATE

更新

So I'm an idiot. Several errors combined on my part led me to believe the correct answers below were not correct. I'd written a PropertyEditorSupportlike this:

所以我是个白痴。我的几个错误使我相信下面的正确答案是不正确的。我写了一个像这样的PropertyEditorSupport

class StringEditor extends PropertyEditorSupport {

    public void setAsText(String text) {
        String value = text.trim();
        if ("" == value) {
            setValue(null);  
        } else {  
            setValue(value);  
        }
    }

}  


There are two problems:


有两个问题:

  1. no getAsText, so my form was getting populated with "null" strings!
  2. my equality check is C++, not Java. When I tried the recommended setter, I just reloaded the post, which already contained the "null" strings. Once I cleaned all that up, everything started working.
  1. 没有 getAsText,所以我的表单填充了“空”字符串!
  2. 我的相等性检查是 C++,而不是 Java。当我尝试推荐的 setter 时,我只是重新加载了已经包含“空”字符串的帖子。一旦我清理了所有这些,一切就开始工作了。

Thanks for the help, and sorry for my "operator error"!

感谢您的帮助,并为我的“操作员错误”感到抱歉!

Brett

布雷特

回答by Affe

The class you're looking for is:

你要找的班级是:

org.springframework.beans.propertyeditors.StringTrimmerEditor

If you construct it with a trueit will convert empty/whitespace strings to null. How to get it registered onto the binder depends on if you want it to be the default or only apply to certain views.

如果你用 a 构造它,true它会将空/空白字符串转换为 null。如何将其注册到活页夹取决于您是希望它成为默认视图还是仅适用于某些视图。

e.g., on a single controller you can just add

例如,在单个控制器上,您只需添加

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}

instructions here

说明在这里

回答by elysch

I know this is old, but I wasted about 2 or 3 hours until I found a very easy way to apply a StringTrimmerEditorwith a binder for all my controllers.

我知道这很旧,但我浪费了大约 2 或 3 个小时,直到找到一种非常简单的方法来StringTrimmerEditor为我的所有控制器应用带有活页夹的 a。

Once again: I must remember to RTFM.

再说一遍:我必须记住要 RTFM。

In spring 3.2 you can create a @ControllerAdvice-annottated controller class and use the @InitBinder-annotated method just like the example @Affe gave.

在 spring 3.2 中,您可以创建一个@ControllerAdvice-annottated 控制器类并使用@InitBinder-annotated 方法,就像@Affe 给出的示例一样。

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-initbinder-advice

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-initbinder-advice

Here is an example:

下面是一个例子:

@ControllerAdvice
@Controller
public class AppBindingInitializer {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

}

Hope it helps someone.

希望它可以帮助某人。

回答by Bozho

Perhaps you can use a custom Binder

也许您可以使用自定义 Binder

回答by blissapp

Set the default value for your optional fields to NULL - actually is it not NULL by default?

将可选字段的默认值设置为 NULL - 实际上默认情况下它不是 NULL?

Parse your input string and then explicitly specify only populated columns with

解析您的输入字符串,然后显式指定仅填充列

 usingColumns

oh, and I'd advise to always sanitise your inputs...

哦,我建议始终消毒您的输入...