Hi,
This can be accomplished by providing a custom PropertyResolver
implementation which catches the IllegalArgumentException and puts its
message in an EvaluationException which is then rethrown:
public void setValue(java.lang.Object base,
java.lang.Object property,
java.lang.Object value)
throws EvaluationException,
PropertyNotFoundException
try {
// set the property
} catch (IllegalArgumentException iae) {
throw new EvaluationException(iae.getMessage(), iae);
} catch (Exception e) {
throw new EvaluationException("blabla", e);
}
The result would be thata the iae.getMessage() is displayed in the
<h:message>
Jurgen
Op di, 20-06-2006 te 09:07 -0400, schreef Bill Schneider:
> Hello,
>
> Has anyone figured out a reasonable way to handle bindings from backing
> beans to "non-anemic" domain models, whose setters may throw exceptions
> when a value breaks some business rule?
>
> For example, imagine an object with an 'employee' property bound to a
> select list
>
> <h:selectOneListBox value="#{backingBean.modelObject.employee}"/>
>
> where the setEmployee() could throw an IllegalArgumentException.
> (invalid employee selected, etc.)
>
> Is there any way to trap that exception and have it be treated the same
> as a validation failure? what I'm trying to do is avoid having to write
> the same business rules in both the presentation layer and business layer.
>
> What's the best practice for handling this? I recognize there may not
> be any good answer, and that this is a universal problem with all
> JavaBeans-oriented web frameworks. It's no different with Struts and
> ActionForms except that JSF handles non-string properties. :-) but I was
> hoping that this would spark some good discussion nonetheless in case
> I'm missing something.
>
> Thanks,
> -- Bill