默认@Fail

入口方法或适配器或其他原因导致异常时需要走的视图,依然是打开MainModule,加入代码

  1. @Fail("jsp:jsp.500")

含义就是内部重定向到/WEB-INF/jsp/500.jsp页面

打开web.xml, 加入如下配置

  1. <error-page>
  2. <error-code>500</error-code>
  3. <location>/WEB-INF/jsp/500.jsp</location>
  4. </error-page>

在WebContent/WEB-INF/jsp/下新建一个文件,叫500.jsp, 内容如下

  1. <%@page import="org.nutz.lang.Strings"%>
  2. <%@page import="java.util.Enumeration"%>
  3. <%@page import="java.io.ByteArrayOutputStream"%>
  4. <%@page import="java.io.PrintWriter"%>
  5. <%@page import="org.nutz.mvc.Mvcs"%>
  6. <%@ page language="java" contentType="text/html; charset=UTF-8"
  7. pageEncoding="UTF-8" isErrorPage="true" trimDirectiveWhitespaces="true"
  8. session="false"%>
  9. <% response.setStatus(500); %>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  14. <title>出错啦</title>
  15. </head>
  16. <body>
  17. <div>
  18. <%
  19. Throwable e = exception;
  20. if (e == null) {
  21. Object obj = request.getAttribute("obj");
  22. if (obj != null && obj instanceof Throwable) {
  23. e = (Throwable)obj;
  24. } else {
  25. if (Mvcs.getActionContext() != null) {
  26. e = Mvcs.getActionContext().getError();
  27. }
  28. }
  29. }
  30. %>
  31. <h2>请求的路径: <%=(request.getAttribute("javax.servlet.forward.request_uri") + (request.getQueryString() == null ? "" : "?" + request.getQueryString())) %></h2><p/>
  32. <%
  33. if (Mvcs.getActionContext() != null) {
  34. %>
  35. <h2>请求的方法: <%=Mvcs.getActionContext().getMethod() %></h2><p/>
  36. <%
  37. }
  38. if (e != null) {
  39. %>
  40. <h2>异常堆栈如下:</h2><p/>
  41. <pre>
  42. <code class="lang-java">
  43. <%
  44. ByteArrayOutputStream bao = new ByteArrayOutputStream();
  45. PrintWriter pw = new PrintWriter(bao);
  46. e.printStackTrace(pw);
  47. pw.flush();
  48. %>
  49. <%=Strings.escapeHtml(new String(bao.toByteArray())) %>
  50. </code>
  51. </pre>
  52. <%
  53. }
  54. %>
  55. </div>
  56. </body>
  57. </html>