SSM框架重构达内NETCTOSS项目——(4)处理异常
来源:互联网 发布:python抓取json数据 编辑:程序博客网 时间:2024/06/10 15:07
处理NETCTOSS项目中的异常
问题:
- 使用@ExceptionHandler处理LoginController中的业务异常:
- 修改LoginController,增加异常处理方法,并使用@ExceptionHandler处理业务异常:
@ExceptionHandlerpublic 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处理业务异常:
@ExceptionHandlerpublic 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>
- 跳转到首页:
0 0
- SSM框架重构达内NETCTOSS项目——(4)处理异常
- SSM框架重构达内NETCTOSS项目——(1)开发准备
- SSM框架重构达内NETCTOSS项目——(2)登录功能
- SSM框架重构达内NETCTOSS项目——(5)资费列表
- SSM框架重构达内NETCTOSS项目——(3)中文乱码
- 关于ssm框架项目,如何进行注解方式异常处理——@ExceptionHandler
- SSM框架全局异常处理的搭建
- ssm框架中的全局异常处理
- SSM项目统一异常处理方案
- NETCTOSS项目(一)
- NETCTOSS项目(二)
- SSM框架项目搭建系列(一)—SSM框架的简介及搭建流程
- 淘淘商城SSM框架之异常处理篇
- SSM框架整合异常
- SSM框架 SSM项目源码 SSM源码
- SSM框架 SSM项目源码 SSM源码
- SSM框架 SSM项目源码 SSM源码
- SSM框架 SSM项目源码 SSM源码
- HTML添加图片
- java面向对象
- 周金涛:人生就是一次康波
- Network In Network笔记-ICLR 2014
- Tomcat 显示日志
- SSM框架重构达内NETCTOSS项目——(4)处理异常
- 检测点2.1
- batch slice layer
- subline Text3 安装 PackControl 报错解决方案
- Android内存优化(上)
- MySQL主主复制以及常见的一些问题
- 2663: [Beijing wc2012]灵魂宝石
- ssh使用的是GB2312编
- Android Studio发布项目到Maven私有库(Nexus)