天天看點

velocity模闆引擎學習(3)-異常處理

velocity模闆引擎學習(3)-異常處理

使用Velocity模闆引擎後,Spring-MVC中異常頁面的處理

按上回繼續,前面寫過一篇Spring MVC下的異常處理、及Spring MVC下的ajax異常處理,今天看下換成velocity模闆引擎後,如何處理異常頁面:

一、404錯誤、500錯誤

1     <error-page>
2         <error-code>404</error-code>
3         <location>/nopage.do</location>
4     </error-page>
5 
6     <error-page>
7         <error-code>500</error-code>
8         <location>/error.do</location>
9     </error-page>      

web.xml中添加這二項,注意locatoion節點,不再是指定成實體檔案路徑,而是Spring MVC中Controller裡具體方法映射的URI

1     @RequestMapping(value = "/nopage.do", method = RequestMethod.GET)
2     public String pageNotFound(Locale locale, Model model) throws Exception {
3         return "errors/404";
4     }
5 
6     @RequestMapping(value = "/error.do", method = RequestMethod.GET)
7     public String innerError(Locale locale, Model model) throws Exception {
8         return "errors/500";
9     }      

上面是Controller的處理

二、正常異常的處理

Controller裡的處理還是跟以前一樣,關鍵是errors/error.vm這個模闆檔案如何寫:

1 <!doctype html>
 2 <html>
 3 <head>
 4     #parse("comm/header.vm")
 5     #set($ex=$request.getAttribute("ex"))
 6     <title>ERROR</title>
 7 </head>
 8 <body style="margin:20px">
 9 <H2>
10     錯誤:$ex.class.simpleName
11 </H2>
12 <hr/>
13 <P>
14     <strong>錯誤描述:</strong>$ex.message
15 </P>
16 
17 <P>
18     <strong>詳細資訊:</strong>
19 </P>
20     <pre>
21 #foreach($stack in $ex.getStackTrace())
22     $stack.toString()
23 #end
24     </pre>
25 </body>
26 </html>      

注意:5、10、21-23這幾行

三、ajax異常的處理

這裡要在BaseController裡直接傳回json字元串,參考下面的代碼:

1    @ExceptionHandler
 2     public String exp(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {
 3         String resultViewName = "errors/error";
 4 
 5         // 記錄日志
 6         logger.error(ex.getMessage(), ex);
 7 
 8         // 根據不同錯誤轉向不同頁面
 9         if (ex instanceof BusinessException) {
10             resultViewName = "errors/biz-error";
11         } else {
12             // 異常轉換
13             //ex = new Exception("伺服器忙,請稍候重試!");
14         }
15 
16         String xRequestedWith = request.getHeader("X-Requested-With");
17         if (!StringUtils.isEmpty(xRequestedWith)) {
18             // ajax請求
19             ResponseUtil.OutputJson(response, "{\"error\":\"" + ex.getClass().getSimpleName() + "\",\"detail\":\"" + ex.getMessage() + "\"}");
20         }
21         request.setAttribute("ex", ex);
22         return resultViewName;
23     }      

關鍵點有2個,方法簽名裡增加HttpServletResponse response,然後19行,直接輸出Json字元串,其中用到了一個ResponseUtil類,該類的主要代碼如下:

1     public static void OutputContent(HttpServletResponse response,
 2             String contentType, String content) throws IOException {
 3         response.setContentType(contentType + ";charset=utf-8");
 4         response.setCharacterEncoding("UTF-8");
 5         PrintWriter out = response.getWriter();
 6         out.println(content);
 7         out.flush();
 8         out.close();
 9     }
10 
11     public static void OutputJson(HttpServletResponse response, String content)
12             throws IOException {
13         OutputContent(response, "application/json", content);
14     }      

作者:菩提樹下的楊過

出處:http://yjmyzz.cnblogs.com

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。

繼續閱讀