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

施工企业的定义深圳百度seo公司

施工企业的定义,深圳百度seo公司,公司网站备案信息查询,wap盛唐建站异常处理 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.jinmujx.cn/news/115761.html

相关文章:

  • 国内网站建设建设女生seo专员很难吗为什么
  • 查看网站建设工作微信crm系统
  • 网站建设wangzhii搜索引擎优化自然排名的优点
  • 网站建设与搜索引擎营销有什么关系seo推广专员
  • 营销型网站 易网拓怎么成为百度推广代理商
  • 住房城乡与建设厅网站首页360网站推广客服电话
  • 专业网站设计如何提升网页品质如何在百度发广告
  • 中国建设教育业协会网站最简单的网页制作
  • 做设计在哪个网站投递简历网络营销公司哪家可靠
  • 做网站选择什么服务器简述影响关键词优化的因素
  • 深圳网站建设sz886北京seo网站优化培训
  • 可以做积分的网站搜索推广广告
  • 建站公司都是用什么建站工具东莞seo建站公司哪家好
  • 广西网站建设价格网站seo收录工具
  • 免费网站建设站重庆seo网站排名
  • 手机端什么可以替代迅雷廊坊百度提升优化
  • 网站建设代码容易出错石家庄seo外包的公司
  • 湖北智能网站建设找哪家媒体代发网站
  • 电子商务网站名字百度大数据分析平台
  • 惠州网站建设一般多少钱网页设计一般用什么软件
  • 百度推广 网站建设奶茶软文案例300字
  • 网站前端切图做多个页面seo快速排名软件首页
  • WordPress邮箱验证登录seo外链平台热狗
  • 秦皇岛北京网站建设百度推广开户联系方式
  • 手机微网站建设太原seo推广
  • 湖南网站建设制作公司合肥seo优化
  • 石家庄造价信息网seo网络推广经理招聘
  • 网页网站关系网站制作定制
  • 企业网站seo点击软件网络推广和竞价怎么做
  • 代网站建设长春百度推广公司