JSF Exception Handling with Spring AOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
configuring exception handling with jsf2.0,
http://jugojava.blogspot.com/2010/09/jsf-2-exception-handling.html