I am creating a form in JSF to save a new entity bean. I binding the properties of the new entity to input elements use the value property e.g. , where backing bean is JSF managed mean and newEntity is filed of the backing bean whcih contains a new instance of my entity. For properties which are value types (like numbers), the input fields will be populated with default values (e.g. 0). I want all the fields to be blank initially and saved back the new entity instance when the page is submitted. I suppose I could make all the properties null able by using types such as Integer but the values aren't null able in the database and it doesn't seem right that the requirements of my user interface should dictate the form of by business layer. I am looking at this the wrong way?
views:
42answers:
1I assume that if the fields aren't nullable, then they are required. If you put required="true"
on the field, then validation should fail before the value can be assigned. This means it won't try to assign null values to your attribute. You may have to write a custom validator to do this for some types of fields.
Alternatively, you can write a converter. This converter would have to convert empty values to some predetermined constant such as -1 that you know is invalid. And vice versa, it would have to convert -1 to empty value for display. Then you have to deal with all the invalid values before sending it to the business layer.
I've used both of these methods, but I much prefer the first. If the fields are not required, they should be nullable in the business layer.