velocity模板引擎学习(3)-异常处理
按上回继续,前面写过一篇Spring MVC下的异常处理、及Spring MVC下的ajax异常处理,今天看下换成velocity模板引擎后,如何处理异常页面:
一、404错误、500错误
<error-page>
<error-code>404</error-code>
<location>/nopage.do</location>
</error-page> <error-page>
<error-code>500</error-code>
<location>/error.do</location>
</error-page>
web.xml中添加这二项,注意locatoion节点,不再是指定成物理文件路径,而是Spring MVC中Controller里具体方法映射的URI
@RequestMapping(value = "/nopage.do", method = RequestMethod.GET)
public String pageNotFound(Locale locale, Model model) throws Exception {
return "errors/404";
} @RequestMapping(value = "/error.do", method = RequestMethod.GET)
public String innerError(Locale locale, Model model) throws Exception {
return "errors/500";
}
上面是Controller的处理
二、常规异常的处理
Controller里的处理还是跟以前一样,关键是errors/error.vm这个模板文件如何写:
<!doctype html>
<html>
<head>
#parse("comm/header.vm")
#set($ex=$request.getAttribute("ex"))
<title>ERROR</title>
</head>
<body style="margin:20px">
<H2>
错误:$ex.class.simpleName
</H2>
<hr/>
<P>
<strong>错误描述:</strong>$ex.message
</P> <P>
<strong>详细信息:</strong>
</P>
<pre>
#foreach($stack in $ex.getStackTrace())
$stack.toString()
#end
</pre>
</body>
</html>
注意:5、10、21-23这几行
三、ajax异常的处理
这里要在BaseController里直接返回json字符串,参考下面的代码:
@ExceptionHandler
public String exp(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {
String resultViewName = "errors/error"; // 记录日志
logger.error(ex.getMessage(), ex); // 根据不同错误转向不同页面
if (ex instanceof BusinessException) {
resultViewName = "errors/biz-error";
} else {
// 异常转换
//ex = new Exception("服务器忙,请稍候重试!");
} String xRequestedWith = request.getHeader("X-Requested-With");
if (!StringUtils.isEmpty(xRequestedWith)) {
// ajax请求
ResponseUtil.OutputJson(response, "{\"error\":\"" + ex.getClass().getSimpleName() + "\",\"detail\":\"" + ex.getMessage() + "\"}");
}
request.setAttribute("ex", ex);
return resultViewName;
}
关键点有2个,方法签名里增加HttpServletResponse response,然后19行,直接输出Json字符串,其中用到了一个ResponseUtil类,该类的主要代码如下:
public static void OutputContent(HttpServletResponse response,
String contentType, String content) throws IOException {
response.setContentType(contentType + ";charset=utf-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(content);
out.flush();
out.close();
} public static void OutputJson(HttpServletResponse response, String content)
throws IOException {
OutputContent(response, "application/json", content);
}
velocity模板引擎学习(3)-异常处理的更多相关文章
- velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools
通常velocity是配合spring mvc之类的框架在web中使用,但velocity本身其实对运行环境没有过多的限制,在单独的java application中也可以独立使用,下面演示了利用ve ...
- velocity模板引擎学习(1)
velocity与freemaker.jstl并称为java web开发三大标签技术,而且velocity在codeplex上还有.net的移植版本NVelocity,(注:castle团队在gith ...
- velocity模板引擎学习(2)-velocity tools 2.0
使用velocity后,原来的很多标签无法使用了,必须借助velocity tools来完成,目前velocity tools最新版本是2.0,下面是velocity tools的一些注意事项: 1. ...
- 使用 Velocity 模板引擎快速生成代码(zhuan)
http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ ****************************************** ...
- 使用Velocity 模板引擎快速生成代码
Velocity 模板引擎介绍 在现今的软件开发过程中,软件开发人员将更多的精力投入在了重复的相似劳动中.特别是在如今特别流行的MVC架构模式中,软件各个层次的功能更加独立,同时代码的相似度也更加高. ...
- Velocity模板引擎语法
Velocity 模板引擎介绍 Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java ...
- Velocity模板引擎入门
类似于PHP中的Smarty,Velocity是一个基于Java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代 ...
- 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用
原文地址http://www.itzhai.com/the-introduction-of-the-velocity-template-engine-template-language-syntax- ...
- 转 如何使用velocity模板引擎开发网站
基于 Java 的网站开发,很多人都采用 JSP 作为前端网页制作的技术,尤其在是国内.这种技术通常有一些问题,我试想一下我们是怎样开发网站的,通常有几种方法: 1:功能确定后,由美工设计网页的UI( ...
随机推荐
- 基于微软平台IIS/ASP.NET开发的大型网站有哪些呢?
首先说明一下,本文绝不是要说Microsoft平台多么好,多么牛.只是要提醒一些LAMP/JAVA平台下的同志们,微软平台不至于像你们说的,和想象的那么不堪!只是你们自己不知道而已.同时,也希望广大M ...
- System.Drawing.Image在Save之后Type变了
前段时间发现asp.net MVC 3附带了一个相当方便的图片处理类WebImage,常用的图片处理功能全都包括进去了,用起来是相当的爽. 在项目中刚好有相关的图片处理需求,但由于实际项目是使用asp ...
- 用CSS3实现背景的固定
今天放假了,正好最近养成了没事泡泡博客园的习惯,自己也有了博客..不得不吐槽一下博客园为什么页面这么古朴,,带的几个模版也没啥意思,反正不符合我口味,幸亏后台提供了编辑CSS的功能,于是我就搬来现有的 ...
- Apache Project SVN Download Sit
apache project svn download sit : http://svn.apache.org/repos/asf 如果想要研究相关项目的源码的话.或者想要成为某个项目的开发者, 就可 ...
- 【Android】apk文件反编译
工具: 压缩软件:用于解压apk包 dex2jar:*.dex反编译为Jar包 jd-gui:查看Jar文件 (PS:不知道博客园如何上传附件,只能把工具挂到百度网盘了:http://pan.baid ...
- ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出
之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itexts ...
- 在Linux下使用gradle自动打包
一.下载软件包 1.下载地址 wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz wget http://services ...
- STL——遍历 删除 set 元素
==================================声明================================== 本文版权归作者所有. 本文原创,转载必须在正文中显要地注明 ...
- 创建mysql存储过程,调用 及删除
//创建表 create table test ( aid ) primary key auto_increment , nickname ), addtime ) ) //查看表结构 show co ...
- <a>标签,鼠标经过或者停留触发延时响应事件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcUAAAEoCAIAAACmeX2PAAAgAElEQVR4nOzdd3xUdb74f3+Pu3v33t ...