How VRaptor3 handle Exception in Controller Methods
3 respostas
H
haiker
Hi, All
I have this situation,
in my controller layer, they may throw exceptions, and I want to handle them in one place, for example, I want to log the exception details and forward the request to a jsp page to display some info to our clients.
is there any ways to satisfy my request?
pls be kindly reply this top in English or Chinese~thanks
So if you only need to logging the error and return to a generic page, you can use the error-page into your web.xml and create a simple interceptor to generate your log. See bellow.
@Intercepts@RequestScopedpublicclassMyLoggingInterceptorimplementsInterceptor{@Overridepublicbooleanaccepts(ResourceMethodmethod){returntrue;// intercept all requests}@Overridepublicvoidintercept(InterceptorStackstack,ResourceMethodmethod,ObjectresourceInstance)throwsInterceptionException{try{stack.next(method,resourceInstance);}catch(Exceptione){// my log code// you can only print the log message or send by email, etc}}}