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.
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?
An IDE is no substitute for an Intelligent Developer.
Joined: Feb 22, 2010
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".
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.
Joined: Feb 22, 2010
Thanks for the reply Tim.
But, I really wish there is a way without creating indicators.
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....
Initialize the variable with some logical value to proceed further.