Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active September 23, 2015 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rponte/595436 to your computer and use it in GitHub Desktop.
Save rponte/595436 to your computer and use it in GitHub Desktop.
JSF Exception Handling with Spring AOP
<beans ...>
<!-- We can use annotations instead of xml <3 -->
<bean id="exceptionHandler"
class="br.com.triadworks.sample.view.jsf.ExceptionHandler"/>
<aop:config>
<aop:aspect id="exceptionHandlerAspect" ref="exceptionHandler">
<aop:pointcut id="managedBeanMethods"
expression="execution(* br.com.triadworks.sample.view.jsf..*.*(..)) and bean(*Bean)"/>
<aop:around pointcut-ref="managedBeanMethods" method="handle" />
</aop:aspect>
</aop:config>
</beans>
package br.com.triadworks.sample.view.jsf;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import br.com.triadworks.sample.view.util.FacesUtils;
public class ExceptionHandler {
/**
* Default error message
*/
public static final String internalErrorMessage = "And internal server error has happened, please contact our support for more details.";
/**
* Handles any exceptions thrown from the managed beans
*/
public Object handle(ProceedingJoinPoint pjp) {
try {
return pjp.proceed();
} catch (Throwable e) {
String message = StringUtils.defaultString(e.getMessage());
if (!message.isEmpty())
message = " [" + message + "]";
FacesUtils.addErrorMessage(internalErrorMessage + message); // Use DI if you can (an example: http://github.com/rponte/jsf-loja-project).
e.printStackTrace();
}
return null; // keeps in the same page
}
}
@rponte
Copy link
Author

rponte commented Sep 20, 2012

configuring exception handling with jsf2.0,
http://jugojava.blogspot.com/2010/09/jsf-2-exception-handling.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment