You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Andy <an...@hotmail.com> on 2008/02/29 19:58:35 UTC

Struts1 forms with empty Strings

Hi,
I was wondering if there is a way to configure Struts1 so Form beans return null values instead of empty String values when a user does not enter data in a text field, for example.  The problem is that Hibernate then saves empty Strings to the database instead of nulls.  I would like to configure something globally if possible.  
Thanks.. Andy

 
_________________________________________________________________
Helping your favorite cause is as easy as instant messaging. You IM, we give.
http://im.live.com/Messenger/IM/Home/?source=text_hotmail_join

Re: Struts1 forms with empty Strings

Posted by Randy Burgess <RB...@nuvox.com>.
Since Strings are immutable you get a new String whenever an operation is
performed on said String, so that seems to me that you would still get the
empty string.

Regards,
Randy Burgess
Sr. Web Applications Developer
Nuvox Communications



> From: Dave Newton <ne...@yahoo.com>
> Reply-To: Struts Users Mailing List <us...@struts.apache.org>
> Date: Fri, 29 Feb 2008 12:53:36 -0800 (PST)
> To: Struts Users Mailing List <us...@struts.apache.org>
> Subject: Re: Struts1 forms with empty Strings
> 
> --- Martin Gainty <mg...@hotmail.com> wrote:
>> //easiest solution would be to set all the attribute default values to null
> 
> Isn't that the default value for a String field? [1]
> 
> Does this prevent and empty value from the HTML form being set on the
> ActionForm?
> 
> Dave
> 
> [1] http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
> 
>> e.g.
>> public class TestFormBean extends ActionForm
>> {
>>     private String   stringProperty=null;
>>     private Object[] arrayProperty=null;
>> 
>>     public  String   getStringProperty()
>>    {
>>      return (stringProperty);
>>     }
>>     public  void   setStringProperty(String stringProperty)
>>     {
>>       this.stringProperty = stringProperty;
>>     }
>> 
>>     public  Object[] getArrayProperty()
>>    {
>>      return (arrayProperty);
>>     }
>>     public  void setArrayProperty(Object[] arrayProperty)
>>     {
>>       this.arrayProperty = arrayProperty;
>>     }
>> }
>> 
>> ----- Original Message -----
>> From: "Andy" <an...@hotmail.com>
>> 
>> I was wondering if there is a way to configure Struts1 so Form beans return
>> null values instead of empty String values when a user does not enter data
>> in a text field, for example.  The problem is that Hibernate then saves
>> empty Strings to the database instead of nulls.  I would like to configure
>> something globally if possible.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 



This email and any attachments ("Message") may contain legally privileged and/or confidential information.  If you are not the addressee, or if this Message has been addressed to you in error, you are not authorized to read, copy, or distribute it, and we ask that you please delete it (including all copies) and notify the sender by return email.  Delivery of this Message to any person other than the intended recipient(s) shall not be deemed a waiver of confidentiality and/or a privilege.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts1 forms with empty Strings

Posted by Dave Newton <ne...@yahoo.com>.
--- Martin Gainty <mg...@hotmail.com> wrote:
> //easiest solution would be to set all the attribute default values to null

Isn't that the default value for a String field? [1]

Does this prevent and empty value from the HTML form being set on the
ActionForm?

Dave

[1] http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

> e.g.
> public class TestFormBean extends ActionForm
> {
>     private String   stringProperty=null;
>     private Object[] arrayProperty=null;
> 
>     public  String   getStringProperty()
>    {
>      return (stringProperty);
>     }
>     public  void   setStringProperty(String stringProperty)
>     {
>       this.stringProperty = stringProperty;
>     }
> 
>     public  Object[] getArrayProperty()
>    {
>      return (arrayProperty);
>     }
>     public  void setArrayProperty(Object[] arrayProperty)
>     {
>       this.arrayProperty = arrayProperty;
>     }
> }
> 
> ----- Original Message -----
> From: "Andy" <an...@hotmail.com>
> 
> I was wondering if there is a way to configure Struts1 so Form beans return
> null values instead of empty String values when a user does not enter data
> in a text field, for example.  The problem is that Hibernate then saves
> empty Strings to the database instead of nulls.  I would like to configure
> something globally if possible.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts1 forms with empty Strings

Posted by Martin Gainty <mg...@hotmail.com>.
//easiest solution would be to set all the attribute default values to null
e.g.
public class TestFormBean extends ActionForm
{
    private String   stringProperty=null;
    private Object[] arrayProperty=null;

    public  String   getStringProperty()
   {
     return (stringProperty);
    }
    public  void   setStringProperty(String stringProperty)
    {
      this.stringProperty = stringProperty;
    }

    public  Object[] getArrayProperty()
   {
     return (arrayProperty);
    }
    public  void setArrayProperty(Object[] arrayProperty)
    {
      this.arrayProperty = arrayProperty;
    }
}

Anyone else?
M-
----- Original Message -----
From: "Andy" <an...@hotmail.com>
To: <us...@struts.apache.org>
Sent: Friday, February 29, 2008 1:58 PM
Subject: Struts1 forms with empty Strings


Hi,
I was wondering if there is a way to configure Struts1 so Form beans return
null values instead of empty String values when a user does not enter data
in a text field, for example.  The problem is that Hibernate then saves
empty Strings to the database instead of nulls.  I would like to configure
something globally if possible.
Thanks.. Andy


_________________________________________________________________
Helping your favorite cause is as easy as instant messaging. You IM, we
give.
http://im.live.com/Messenger/IM/Home/?source=text_hotmail_join


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts1 forms with empty Strings

Posted by Tim B <ti...@shaw.ca>.
"Dave Newton" <ne...@yahoo.com> wrote in message
news:362194.56664.qm@web56705.mail.re3.yahoo.com...
> --- Andy <an...@hotmail.com> wrote:
> > I was wondering if there is a way to configure Struts1 so Form beans
return
> > null values instead of empty String values when a user does not enter
data
> > in a text field, for example.  The problem is that Hibernate then saves
> > empty Strings to the database instead of nulls.  I would like to
configure
> > something globally if possible.
>
> I don't know if this is possible without writing some code, like a request
> processor; hopefully somebody else knows more. The issue is that a text
field
> on a form returns an empty string, not null: that's an HTTP thing, not
Struts
> specifically.
>
> You could also configure Hibernate data types that convert an empty string
in
> to null and handle the problem on the Hibernate side; if you're using the
> Hibernate code anywhere else it might be more useful to do it that way,
but
> it's borderline magic and might be confusing down the road.
>
> Dave

A filter that intercepts the request and removes all parameters with empty
values should do the trick. I have used a similar filter to trim all the
reqest parameters of white space rather than doing it in the form bean
setter methods.




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts1 forms with empty Strings

Posted by Dave Newton <ne...@yahoo.com>.
--- Andy <an...@hotmail.com> wrote:
> I was wondering if there is a way to configure Struts1 so Form beans return
> null values instead of empty String values when a user does not enter data
> in a text field, for example.  The problem is that Hibernate then saves
> empty Strings to the database instead of nulls.  I would like to configure
> something globally if possible.  

I don't know if this is possible without writing some code, like a request
processor; hopefully somebody else knows more. The issue is that a text field
on a form returns an empty string, not null: that's an HTTP thing, not Struts
specifically.

You could also configure Hibernate data types that convert an empty string in
to null and handle the problem on the Hibernate side; if you're using the
Hibernate code anywhere else it might be more useful to do it that way, but
it's borderline magic and might be confusing down the road.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org