3 Replies Latest reply on Jul 2, 2009 4:00 AM by Dan Keyes

    Conversions in setXXX and getXXX methods

    Dan Keyes Newbie

      I would like to read a parameter from an inputText field as a String and then convert it to an int.  I would like to use a String in the inputText field so that the user can leave that field blank.  If the parameter is a numeric value, then it can’t be left blank.   Instead, it will always have some value such as zero.  The obvious place to convert the String to an int would be in the setXXX method.  I know that Dan Allen once mentioned that setXXX methods should contain no business logic, because JSF and Seam invoke the set/get methods frequently.  Although I agree with the no business logic in the set/get methods idea, 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 there, but I’m using a subclass of EntityQuery, so I would have to override a method of that super class.  I suppose it would be getResultList().  What is the “best practice” for doing conversions with a subclass of EntityQuery?

        • 1. Re: Conversions in setXXX and getXXX methods
          Tim Evers Master

          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 the int 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 there

          The 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
            Cody Lerum Apprentice

            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
              Dan Keyes Newbie

              Thank you guys for that great information!