Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.