SSM框架重构达内NETCTOSS项目——(4)处理异常

本文介绍如何在NETCTOSS项目中通过@ExceptionHandler注解处理LoginController中的业务异常及使用SimpleMappingExceptionResolver处理系统异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

处理NETCTOSS项目中的异常

  1. 问题:


  2. 使用@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;
          }
      }

  3. 使用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>

  4. 在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>
      


  5. 测试:
    • 跳转到首页:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值