FacesMessages lost when handling an exception
so38 Aug 18, 2008 11:19 PMI am trying to handle some ConstraintViolationExceptions in the code so that I can provide a user friendly error message. After I add the FacesMessages I throw the exception again so that seam automatically rollsback the transaction. The problem is that the FacesMessages that I added are not displayed on the page only the messages that was provided in pages.xml for the exception. Am I doing something wrong? Have others experienced this and is this a bug in seam? I am using the latest SNAPSHOT of 2.0.3
Here is an example of what I am doing:
Here is my action
public String register() {
try{
user.setPassword(DigestUtils.shaHex(user.getPassword()));
user.setEnabled(true);
theSession.persist(user);
theSession.flush();
}catch(ConstraintViolationException e){
@SuppressWarnings("unchecked")
Map<String, String> violatedUniqueConstraints = (Map<String, String>) theSession.createQuery("select new map(username as username, email as email) from User u " +
"where u.username = #{user.username} or u.email = #{user.email}").uniqueResult();
if(violatedUniqueConstraints.containsKey("username") && violatedUniqueConstraints.get("username").equals(user.getUsername())){
facesMessages.add("User with username #{user.username} already exists");
}
if(violatedUniqueConstraints.containsKey("email") && violatedUniqueConstraints.get("email").equals(user.getEmail())){
facesMessages.add("User with email #{user.email} already exists");
}
//Rethrow the exception so the transaction is rolled back
throw e;
}
}
then in pages.xml if have the following so that it is redirected to the same page it was on
<exception class="org.hibernate.exception.ConstraintViolationException"> <redirect> <message/> </redirect> </exception>
The interesting thing is that if I call
FacesManager.instance().redirect(Pages.getCurrentViewId());
before I rethrow the exception all messages are displayed correctly and the code in org.jboss.seam.web.ExceptionFilter.endWebRequestAfterException is also called which is what rolls back the transaction.