java - How I can catch exception in JSP files? -
i used exceptionhandler catch exception in controllers.
@exceptionhandler(value = {exception.class, runtimeexception.class}) public final modelandview globalexceptionhandler(final exception exception) { modelandview modelandview = new modelandview("error/500"); modelandview.addobject("tl_exception", errorsystem.processingerror(exception)); return modelandview; }
but, example, if in jsp file tring data null object, that's exception not cathcing.
i need advice, how can catch exception in jsp file? or error need catching in controllers?
update:
the best solution put in web.xml uri errors.
<error-page> <location>/error</location> </error-page>
after create controller needed processing error request:
@controller public final class errorcontroller { @requestmapping(value = "/error") public final modelandview globalerrorhandle(final httpservletrequest request) { string page = "error/500"; final string code = request.getattribute("javax.servlet.error.status_code").tostring(); if (null != code && !code.isempty()) { final integer statuscode = integer.parseint(code); switch (statuscode) { case 404 : page = "error/404"; case 403 : page = "error/403"; } } return new modelandview(page); } }
adding @astrohome answer, jsp
gives option specify error page each jsp. whenever page throws exception, jsp container automatically invokes error page.
to set error page, use <%@ page errorpage="xxx" %>
directive.
and on error handling jsp mentioned above include directive <%@ page iserrorpage="true" %>
.
example, let's have jsp page name main.jsp
on trying perform operation on null object.
main.jsp
<%@ page errorpage="show-error.jsp" %> <html> <head> <title>page on error occurs</title> </head> <body> </body> </html>
show-error.jsp
<%@ page iserrorpage="true" %> <html> <head> <title>show error</title> </head> <body> <p>exception stack trace:<% exception.printstacktrace(response.getwriter()); %> </p> </body> </html>
Comments
Post a Comment