-
1. Re: Conversions in setXXX and getXXX methods
kragoth Jul 2, 2009 1:16 AM (in response to noonereally)I've never experience a problem with leaving integer fields blank. I think you should make sure you are not setting a default value in your constructor or doing something weird when you come across a null value. (Try using
Integer
type instead of theint
type by the way, they are very different)In my opinion it would be a bad idea to bind your inputText to a String field and they try to convert it after the update model phase.
But anyway if you can't find why you have zero in your int field then the answer to your question is actually in what you said.
You said
I’m not sure where I should put the code to do the conversion. If I had written the action method, then I would think about putting the conversion code thereThe answer to your problem is to write a converter because if you want conversion logic then put it in the place where conversion happens.
So write a converter like this: (although SEAM doco has good examples you should read)
@Name(TextLookupConverter.SEAM_ID) @Converter @BypassInterceptors public class MyConverter implements javax.faces.convert.Converter { @Override public String getAsString( FacesContext context, UIComponent component, Object value) { //logic to convert your object into a string } @Override public Object getAsObject( FacesContext context, UIComponent component, String value) { //logic to convert your string to your object } }
After you have written a converter you need to register it etc etc, but please read the doco for all that first.
For basic types like Integer, String, etc you should not need to write your own converter. JSF/SEAM should just work out of the box for these types. So I think you should make sure you are using Integer instead of int and see if this solves your problem first, before going off and writing converters.
-
2. Re: Conversions in setXXX and getXXX methods
clerum Jul 2, 2009 3:11 AM (in response to noonereally)Like Tim said you need to use and Interger NOT and int
Interger's can be null;
int's default to zero
-
3. Re: Conversions in setXXX and getXXX methods
noonereally Jul 2, 2009 4:00 AM (in response to noonereally)Thank you guys for that great information!