Error handling - conceptual question
donestebanc Jan 25, 2011 6:04 PMHi all,
hopefully, my question fits in this forum.
I found a lot of information about Converters and Validators and I think I understand the basic concept. But I don't know where/how to handle errors, occurring later in the JSF lifecycle? Imagine inserting a new user in a database. First passing Converters and Validators, to check that basic information and constraints like proper date values or reasonable age ranges are given. But where to check constraints which needs backend information like "no duplicate user name permitted".
One place to detect this kind of error is the action method in the corresponding form bean. The action method calls a database service, which returns success or failure or throws an exception. It would not help anticipate this check in a validator. The check for the duplicate user must be in the same Transaction as the insert.
I tried something like this in the action method:
public String update() throws LoginNameExistsException {
try {
DSServices.updateUser(currentUser);
} catch (LoginNameExistsException e) {
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage("password", new FacesMessage(FacesMessage.SEVERITY_ERROR, "short", "looooong"));
fc.validationFailed();
}
return null;
}
First of all, this does not work. I get this message in the log:
INFO: WARNUNG: FacesMessage(s) wurde(n) in die Warteschlange gestellt, aber möglicherweise nicht angezeigt.
Which means that the message is in the queue but not displayed.
Client Code:
<h:inputText id="name" label="Login-Name" value="#{adminUsersBean.currentUser.loginName}" />
<h:message for="name" styleClass="errorMessage" />
Other messages from Validators are displayed correct.
Second it just doesn't feel right manipulating messages in the action methods. But thats just a feeling.
I don't know how to handle situations like this and I did not find information about this task, neither in my book nor on the web or the examples. Perhaps someone can point me to missing information.
I'm aware, that this is not primary a RichFaces question, but I participate only in this forum. If someone could recommend an active forum about JSF2, it would be fine too.
Thanks
Don