You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Van Wart <sc...@indosoft.ca> on 2006/02/20 00:59:53 UTC
Action forms and numeric coercion
I'm new to struts and am setting up some form beans. I'm using
hibernate, and one of my tables has a lot of foreign keys. The target
tables are pretty much just lookup tables, which I use to populate a
good number of drop-downs on my web page. Most of the foreign keys are
optional, and I'm storing the values in the action form as java.lang.Long.
The optional dropdowns all have <option value="">None</option>, but when
the form is submitted, and the empty string is coerced to a
java.lang.Long, it gives me a value of 0 instead of null. Am I going
about this the right way? Should I just suck it up and handle the 0
value, or is there a better way to lay out my form bean's class?
Thanks,
Scott
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Action forms and numeric coercion
Posted by Laurie Harper <la...@holoweb.net>.
Scott Van Wart wrote:
> I'm new to struts and am setting up some form beans. I'm using
> hibernate, and one of my tables has a lot of foreign keys. The target
> tables are pretty much just lookup tables, which I use to populate a
> good number of drop-downs on my web page. Most of the foreign keys are
> optional, and I'm storing the values in the action form as java.lang.Long.
>
> The optional dropdowns all have <option value="">None</option>, but when
> the form is submitted, and the empty string is coerced to a
> java.lang.Long, it gives me a value of 0 instead of null. Am I going
> about this the right way? Should I just suck it up and handle the 0
> value, or is there a better way to lay out my form bean's class?
That's one of the many reasons why it's strongly recommended that you
use String-type properties in your form beans. Alternatively, if you
really don't want to use strings, you can configure BeanUtils (which
Struts is using under the covers to populate the form bean) to give you
the null value you're expecting.
To do that, you need to construct a replacement converter with 'new
LongConverter(null)' and register it as the converter for Long.TYPE and
Long.class. [Note, I haven't tested if specifying a default value of
null will work; you may have to specify an actual instance of Long,
instead.]
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org