Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom approach to centralize messages for the views  RSS feed

 
Enrique Medina
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Taking a look at several sample code, I see that in every action throughout the managed beans, the developer has to capture several exceptions. This can be tedious and repetitive.

So what I propose is to use a callback mechanism in order to provide with a centralized point to capture all exceptions that might be thrown inside the JSF layer. The idea is to extend an AbstractBaseBean that will have the following protected method:

protected Object execute(ActionCallback action)
{
Object result = null;

try
{
result = action.doExecuteAction(this.boDelegate);
}
catch (BusinessException be)
{
FacesUtils.manageViewExceptions(be);
}

return result;
}

As you can see, I make sure that no other than BusinessExceptions arrive to the JSF layer (if any other exception, like ServletException, is thrown, then the container itself treats them and redirects to an error page). So whatever exception may be thrown by the lower layers, it is captured here and treated in the FacesUtils.manageViewExceptions method.

This method manages exceptions with levels, that determine whether a exception is a 'recoverable' one for the user (e.g. validation error, object not found, warn message), or is a 'non-recoverable' one (e.g. JDBC connection failure). In case it is recoverable, then I create a FacesMessage (SEVERITY_INFO) with the correspondent message from the exception. And the lifecycle goes on...

But if it isn't recoverable, then I generate a FacesMessage (SEVERITY_ERROR) and then I make a hack in the lifecycle doing this:

NavigationHandler navigationHandler = getApplication().getNavigationHandler();
navigationHandler.handleNavigation(FacesContext.getCurrentInstance(), null, NavigationResults.ERROR);

With this technique, I can force the next view to be the error page, as it must be that page the one to show next due to the 'non-recoverable' error.

What do you think about this approach? Am I breaking any rule? What about the hack to force navigation to the error page?

Looking forward to hearing your comments.
 
jaini Naveen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To display the messages, there is a post describes and gives a readymade messages component.
this component will display the messages in the messages in javascript alert
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!