当前位置: 首页 > news >正文

重庆低价网站建设上海优化外包公司排名

重庆低价网站建设,上海优化外包公司排名,404网站页面进入,网站模板建站教程视频教程异常处理 SpringMVC处理异常的方式有三种&#xff0c;当然也可以使用AOP面向编程&#xff0c;自定义一个类进入切入。 第一种&#xff1a;使用SpringMVC提供的简单异常处理器SimpleMappingExceptionResolver <!--SpringMVC提供的异常处理器类型&#xff1a;SimpleMappingE…

异常处理

SpringMVC处理异常的方式有三种,当然也可以使用AOP面向编程,自定义一个类进入切入。

第一种:使用SpringMVC提供的简单异常处理器SimpleMappingExceptionResolver

<!--SpringMVC提供的异常处理器类型:SimpleMappingExceptionResolver-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
   <!--  异常映射属性: 是一个map散列表 用于配置不同异常跳转到不同页面      -->
   <property name="exceptionMappings">
      <props>
         <!--key:  用于指定异常类型,  value用于指定跳转的页面名称-->
         <prop key="java.lang.Exception">error</prop>
      </props>
   </property>
</bean> 

第二种:实现HandlerExceptionResolver接口,自定义异常处理器,并注册

/**
 * 用户【】 IP[]
 * 在【时间】
 * 操作【Controller.find】 发生如下异常
 *   xxxxxxxxxxxxxxxxxxxxx
 *   yyyyyyyyyyyyyyyyyyyyy
 *
 */ 

package com.ssm.netctoss.util;

import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("---------执行了自定义异常处理器------------");
        // 根据不同的异常类型,设置不同的响应状态码
        if (e instanceof MyCustomException) {
            response.setStatus(HttpStatus.BAD_REQUEST.value());
            // 可以添加更多的自定义处理逻辑
            System.out.println("-----");
        }else if (e instanceof IllegalArgumentException) {
            //
        }else {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            // 记录日志或其他处理逻辑
        }
        // 可以将异常信息放入模型中供视图展示
        modelAndView.addObject("errorMessage", e.getMessage());

        // 设置视图名称
        modelAndView.setViewName("error");

        return modelAndView;
    }
}
class MyCustomException extends Exception {
    public MyCustomException(String message) {
        super(message);
    }
}
 

xml配置文件添加: 

 <bean class="com.ssm.netctoss.util.MyExceptionResolver"/> 

 第三种:使用@ExceptionHandler注解实现异常处理

1. 编写如下方法
2. 在方法上添加注解
3. 其他Controller继承即可

package com.ssm.netctoss.util;

import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
    @ExceptionHandler
    public String executeEx(HttpServletRequest request,Exception e){
//        request.setAttribute("msg", e.getMessage());
//        request.setAttribute("code", 1);
        System.out.println("---------------注解异常");
        //根据不同异常类型,返回不同视图
        return "error";
    }
}

第四种:使用AOP,自定义异常处理类型  

 可以正常统一处理异常信息 并且 开启注解扫描,别忘记了 

package com.ssm.netctoss;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.ssm.netctoss.pojo.Admin;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


/**
 * 用户【】 IP[]
 * 在【时间】
 * 操作【Controller.find】 发生如下异常
 *   xxxxxxxxxxxxxxxxxxxxx
 *   yyyyyyyyyyyyyyyyyyyyy
 *
 */
@Component
@Aspect
public class ExceptionLogger {
    @Resource
    private HttpServletRequest request;
    @Around("within(com.ssm.netctoss.controller..*)")
    public Object log(ProceedingJoinPoint p) throws Exception{
        Object obj = null;
        try {
            obj = p.proceed();
        } catch (Throwable e) {
            // 记录异常信息
            Admin admin = (Admin)request.getSession().getAttribute("LOGINADMIN");
            String msg="";
            if(admin!=null){
                String adminCode = admin.getAdminCode();
                String ip = request.getRemoteHost();
                String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                String className = p.getTarget().getClass().getName();
                String methodName = p.getSignature().getName();
                msg+="用户["+adminCode+"],IP["+ip+"],在"+now+"],操作["+className+"."+methodName+"]时,发生如下异常:\n";
            }
            StackTraceElement[] elems = e.getStackTrace();
            for(StackTraceElement elem:elems){
                msg+="\t"+elem.toString()+"\n";
            }
            Logger logger = Logger.getLogger(ExceptionLogger.class);
            logger.error(msg);
            //记录日志后,抛出异常,交给后面的代码继续处理
            throw new Exception(e);
        }
        return obj;
    }
}
 

http://www.hkea.cn/news/2595/

相关文章:

  • 网站服务内容最近新闻大事件
  • 怎么做公司网站google官方下载安装
  • 建个网站需要投资多少百度搜索引擎介绍
  • 速购单微信小程序怎么做白山seo
  • 昵图网免费素材图库官网宁波外贸网站推广优化
  • 网络营销的发展现状如何windows优化大师破解版
  • 怎样学做网站成都seo达人
  • 沧州响应式网站开发搜索引擎优化的基本手段
  • 网站建设的三要素营销推广
  • 怎么上传自己做的网站宿州百度seo排名软件
  • 怎么做网站优化 s网络优化app
  • 平凉哪家做企业网站链接买卖平台
  • 为什么要用国外服务器做网站网络营销推广优化
  • 阜南做网站公司郑州网站推广公司排名
  • 网站后台代码如何做网站怎么优化排名
  • 泉州建站模板系统小红书关键词搜索量查询
  • 高端医院网站建设网络媒体
  • wordpress 二级域名附件如何做网站优化
  • 网站域名在哪里申请品牌营销策划十大要点
  • 网站制作方案包括哪些内容正规的推文平台
  • 做域名不做网站可以吗深圳知名seo公司
  • 招聘网站建设人员条件品牌营销策划包括哪些内容
  • 网站域名证书怎么获取友情链接名词解释
  • 太原网站建设价格什么优化
  • 网站制作公司昆明swot分析
  • 个人养老金制度将推网站推广优化网址
  • 寺庙建设网站的意义2024年度关键词
  • 北京网站开发优选ls20227全国唯一一个没有疫情的城市
  • 那个网站做淘宝推广比较好免费二级域名生成网站
  • 网站空间租用费用网站的开发流程