I have a JSF 2.2 application where the user has to input BigDecimal
values in an <h:inputText>
. For such an input-field, a valueChangeListener
is configured to be called on input changes. Here is the XHTML code:
<h:form id="theForm">
<h:inputText id="bd" value="#{bean.bd}"
valueChangeListener="#{bean.bdChangedListener()}"/>
<h:commandButton id="submit" value="Submit" />
</h:form>
This works fine in most cases. The bdChangedListener()
method is called, when the value changed and the submit-button is pressed. The value is correctly committed to the model.
However, if I entered 1.1
and change it to 1.10
, the new value is committed to the model, but the valueChangeListener
is never called! Debuging showed, that the reason for this is in javax.faces.component.UIInput#compareValues()
. The JavaDoc from
this method says:
Return true if the new value is different from the previous value. First compare the two values by passing value to the equals method on argument previous. If that method returns true, return true. If that method returns false, and both arguments implement java.lang.Comparable, compare the two values by passing value to the compareTo method on argument previous. Return true if this method returns 0, false otherwise.
So it seems to me, this is intentionally. But why?
The user-input changed, and there are applications, where the scale of a BigDecimal
is relevant. So JSF should not just ignore the changed input but notify me! It updates the model with the new value, why would it skip the valueChangeListener-method?
How could I avoid this behavior and get notified in a clean way? (I know I could hook into the setter, but that's not what I call a clean way!)
Any ideas?
Further reading and comments
In addition to the above I want to mention, that I've already read questions like BigDecimal equals() versus compareTo(). I do understand why BigDecimal's equals()
and compareTo()
behave like they do and I'd say it is correct. The behavior of BigDecimal is not the problem, the UIInput.compareValues()
is the problem.
Also a converter (as suggested in comments or already deleted answers) won't save my day. The user input is correctly converted and I need the BidDecimal including the exact scale in my application. Any converter returning a BigDecimal won't change the observed behavior.
A wrapper class around BigDecimal could possibly solve my problem, but is not what I consider a clean solution. I really want to know why UIInput behaves the way it does.
ValueChangeListener
.<h:input>
? The BigDecimal object or the number represented by it? But I got your point and that might indeed be the reason for the observed behavior. Which I still find very unintuitive.