处理NETCTOSS项目中的异常
-
问题:

- 使用@ExceptionHandler处理LoginController中的业务异常:
- 修改LoginController,增加异常处理方法,并使用@ExceptionHandler处理业务异常:

@ExceptionHandler
public String execute(HttpServletRequest request,HttpServletResponse response, Exception e) throws Exception {
if(e instanceof AdminCodeException || e instanceof PasswordException) {
//业务异常自己处理
request.setAttribute("message", e.getMessage());
request.setAttribute("adminCode", request.getParameter("adminCode"));
request.setAttribute("password", request.getParameter("password"));
return "main/login";
} else {
//系统异常抛出,交给Spring处理
throw e;
}
}
- 使用SimpleMappingExceptionResolver处理系统异常:
- 配置spring-mvc.xml,增加SimpleMappingExceptionResolver
<!--处理系统异常-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">main/error</prop>
</props>
</property>
</bean>
- 在WEB-INF/jsp/main下,创建系统错误页面error.jsp,代码如下:
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>达内-NetCTOSS</title>
<!--
当前: netctoss-trySSM/login/checkLogin.do
目标: netctoss-trySSM/styles/global.css
-->
<link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" />
<link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" />
<script language="javascript" type="text/javascript">
var timer;
//启动跳转的定时器
function startTimes() {
timer = window.setInterval(showSecondes,1000);
}
var i = 5;
function showSecondes() {
if (i > 0) {
i--;
document.getElementById("secondes").innerHTML = i;
}
else {
window.clearInterval(timer);
/*
目标: netctoss-trySSM/login/toLogin.do
*/
location.href = "/netctoss-trySSM/login/toLogin.do";
}
}
//取消跳转
function resetTimer() {
if (timer != null && timer != undefined) {
window.clearInterval(timer);
location.href = "/netctoss-trySSM/login/toLogin.do";
}
}
</script>
</head>
<body class="error_page" onload="startTimes();">
<h1 id="error">
遇到错误, <span id="secondes">5</span> 秒后将自动跳转,立即跳转请点击
<a href="javascript:resetTimer();">返回</a>
</h1>
</body>
</html>
- 测试:

- 跳转到首页:

问题:
- 修改LoginController,增加异常处理方法,并使用@ExceptionHandler处理业务异常:
@ExceptionHandler public String execute(HttpServletRequest request,HttpServletResponse response, Exception e) throws Exception { if(e instanceof AdminCodeException || e instanceof PasswordException) { //业务异常自己处理 request.setAttribute("message", e.getMessage()); request.setAttribute("adminCode", request.getParameter("adminCode")); request.setAttribute("password", request.getParameter("password")); return "main/login"; } else { //系统异常抛出,交给Spring处理 throw e; } }
- 配置spring-mvc.xml,增加SimpleMappingExceptionResolver
<!--处理系统异常--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception">main/error</prop> </props> </property> </bean>
<%@ page pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>达内-NetCTOSS</title> <!-- 当前: netctoss-trySSM/login/checkLogin.do 目标: netctoss-trySSM/styles/global.css --> <link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" /> <script language="javascript" type="text/javascript"> var timer; //启动跳转的定时器 function startTimes() { timer = window.setInterval(showSecondes,1000); } var i = 5; function showSecondes() { if (i > 0) { i--; document.getElementById("secondes").innerHTML = i; } else { window.clearInterval(timer); /* 目标: netctoss-trySSM/login/toLogin.do */ location.href = "/netctoss-trySSM/login/toLogin.do"; } } //取消跳转 function resetTimer() { if (timer != null && timer != undefined) { window.clearInterval(timer); location.href = "/netctoss-trySSM/login/toLogin.do"; } } </script> </head> <body class="error_page" onload="startTimes();"> <h1 id="error"> 遇到错误, <span id="secondes">5</span> 秒后将自动跳转,立即跳转请点击 <a href="javascript:resetTimer();">返回</a> </h1> </body> </html>
- 跳转到首页: