• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Avoiding Default primitive wrapper value display on JSF page

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have some properties on the backing bean defined as Character, Integer wrapper objects. When I do not pass any values on the page, default values for these properties are automatically being set (Ex: If I do not pass Integer value, the backing bean property is set to default value '0'). I do not want to change all the properties in the bean to String to avoid this. Please let me know if there is anyway to avoid the JSF setting the default values for the Wrapper object properties in the backing bean.

Thanks,
Ganesh
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid I don't understand. If you define a property, it has to have some value, even if that value is only null. What exactly are you asking?
 
Ganesh Podaralla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Assume that the below is my backing bean.

public class BackingBean
{
private Integer age;
//And the Getters and Setters for code
}

There is an input text box on the JSF page to set the "age" property. This is an optional field on the page (The user may/may not enter the field). If the user does not enter any value on the page, I thought that the "age" property will be null since I am using a Wrapper object (Integer) instead of primitive (int). But the JSF framework is setting the default int value (zero). I am looking for a mechanism where if the user does not enter any value on the page for "age", the "age" property should be null instead of default value "0".

Hope I am clear now.

Thanks,
Ganesh
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, Ganesh. JSF doesn't really believe in "null". For one thing, "null" doesn't translate well to text and even less well in the other direction, where it's hard to tell "null" from a String with the value of "null". HTTP is a text protocol and it doesn't like binary values except via Base64 encoding (which the standard controls don't employ).

Null is a very dangerous thing to use casually for this and other reasons - not just in JSF, so it's generally better to keep a separate indicator for whether or not an object's value should be optional.
 
Ganesh Podaralla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Tim.

But, I really wish there is a way without creating indicators.

Ganesh
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh,

I notice that you have not intialized the variable.
It is fine if you have not recieved a null pointer exception, otherwise it would be prudent to provide an initial value.

As you mentioned earlier, since this is an optional field, the best thing would be to performa a validation at the place where you actually start doing something with this variable.

It is just that if you intialize the variable, we can always have a comaparision point, wherin we can check if the state of the variable has changed or not. That will of course depend on what has been entered in the JSF screen.

This here can be used as a validation condition, which can be inserted before proceeding with operation on the variable.

The validation can be -

If (value of variable has changed ) proceed....
else
Initialize the variable with some logical value to proceed further.

Hope i was helpful.

cheers!
 
reply
    Bookmark Topic Watch Topic
  • New Topic