Meaningless Drivel is fun!*
The moose likes JSF and the fly likes Avoiding Default primitive wrapper value display on JSF page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Avoiding Default primitive wrapper value display on JSF page" Watch "Avoiding Default primitive wrapper value display on JSF page" New topic
Author

Avoiding Default primitive wrapper value display on JSF page

Ganesh Podaralla
Greenhorn

Joined: Feb 22, 2010
Posts: 23
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
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16141
    
  21

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?


Customer surveys are for companies who didn't pay proper attention to begin with.
Ganesh Podaralla
Greenhorn

Joined: Feb 22, 2010
Posts: 23
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

Joined: Jun 25, 2001
Posts: 16141
    
  21

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

Joined: Feb 22, 2010
Posts: 23
Thanks for the reply Tim.

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

Ganesh
shashank nagabhushan maddali
Greenhorn

Joined: Jun 23, 2008
Posts: 5
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Avoiding Default primitive wrapper value display on JSF page