tags:

views:

178

answers:

3

Is there an easy way to turn empty form input into null strings in java? I'm using spring mvc and SimpleJdbcInsert to insert the object into a MySQL database. I'd like to set the blank input to NULL in 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.

Thanks!

edit - 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 propertyEditorSupport like this:

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

Two problems - first, no getAsText, so my form was getting populated with "null" strings! 2nd, 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. Thanks for the help, and sorry for my "operator error"!

Brett

A: 

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

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

 usingColumns

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

blissapp
Mind telling me how to set the default value to NULL? I'm getting empty strings back, so I'm guessing NULL isn't the default.
Brett Stottlemyer
I think it's the default, but you can also add an explicit DEFAULT NULL clause to each field.http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.htmlNB - Note my strikethru - I read this initially outside the context of the SimpleJdbcInsert class
blissapp
Ok, I have default NULL for the database. I thought you were refering to a way to set null as the default java String result for empty input on the form! The problem with usingColumns (I think) is that it takes (String ...) as the arguments - I need to hardcode them, not create a list of string arguments dynamically at runtime by checking the values. I could also have the getters return null for empty strings, which seems comparable, but again means checking every value.
Brett Stottlemyer
+2  A: 

Perhaps you can use a custom Binder

Bozho
I tried that, but it returns a String. Trying setValue(null) left me with Strings == "null" (the four letter word "null", not the value null). Not quite what I was looking for!
Brett Stottlemyer
can't you return `null` String?
Bozho
Custom binder is right. Where you see this `Strings == "null"`; what are you doing? Are you building a SQL string, if so DONT. Use a Preprable.
Justin
Does this only work in older versions of Spring? I'm using 2.5.5 and it isn't working, even using the StringTrimmerEditor also suggested. The object I'm binding to is getting a String with the value of "null". I can pass the object back to a new webpage and I will have the text "null", so this isn't related to SQL at all. So I'm stuck thinking this only works for older versions of Spring. what else could it be?
Brett Stottlemyer
+6  A: 

The class you're looking for is:

org.springframework.beans.propertyeditors.StringTrimmerEditor

If you construct it with a true it 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.

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

Affe
See my comments to Bozho regarding registerCustomEditor not working in Spring 2.5.5
Brett Stottlemyer
Well, I can't swear on 2.5.5, but I am quite sure on 2.5.6 and 3 that it sets the string to the null reference, it doesn't set a string with a value of "null." Perhaps if you added the offending code to your question we could help more?
Affe
See my edit to the original post. My bad, thanks for your patience.
Brett Stottlemyer