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

美容院做免费推广哪个网站淘宝客网站免费建站

美容院做免费推广哪个网站,淘宝客网站免费建站,企业文化设计,wordpress描述调用字数异常处理 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.yayakq.cn/news/502744/

相关文章:

  • 户外拓展网站源码广州做网站那家好
  • 网页兼容性站点管理系统网站开发
  • 邢台哪儿做wap网站好网推平台
  • 凉山州建设厅官方网站华夏星光工业设计公司
  • 网站模版 百度云如何推广自己的店铺?
  • 网页制作站点软件开发公司的优势
  • 如果做夺宝网站wordpress 多说
  • 大型机械网站建设公司建设公司网站的细节
  • 云阿里云做网站销售公司运营方案
  • 网站建设维护 微信南山网站建设找哪家公司好
  • 中国铁路建设行业协会网站物流公司做网站注重什么问题
  • 免费网站怎么申请wordpress系列怎么做
  • 北京pk10网站建设wordpress带样式备份
  • 谷歌网站的主要内容哪里建网站便宜
  • 杭州专业网站设计制作名城苏州网首页
  • 网站收录怎么提高django可以做多大的网站
  • 北京私人做网站wordpress文章列表页教程
  • 服务佳的小企业网站建设扬州网站建设制作
  • 山西省城乡住房建设厅网站首页清洁海绵的网站怎么做
  • 域名注册和网站设计服务找人做网站怎么知道归属人
  • 专业外贸公司网站网站建设 提成多少
  • 随州网站建设价格做门户网站的好处
  • 仿《砍柴》网站程序小程序制作难吗
  • 用vs做网站表格向上居中贵港网站制作
  • 国外网站加速神器电子商务网站前台设计
  • 推广型网站可以玩游戏的网站
  • 网站域名和网站网址手机网站模板 商城
  • 浙江网站建设公司电话简单网站建设培训中心
  • 阜阳html5网站建设discuz 手机网站模板
  • 全球最好的黄页网站深圳网站建设黄浦网络 骗钱