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

wordpress页尾添加信息长沙百家号seo

wordpress页尾添加信息,长沙百家号seo,什么网站自己做名片好,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.dt0577.cn/news/29901.html

相关文章:

  • jsp网站开发介绍创建网站的基本流程
  • 网站视频站建设教程和seo推广软件费用
  • 北京app定制开发公司seo公司 杭州
  • 提供企业网站建设定制12345浏览器
  • 个人门户网站seo网站查询工具
  • dede微电影网站模板下载安卓aso关键词优化
  • 创造与魔法官方网站一起做喜欢的事苏州做网站的专业公司
  • 做网站是域名怎么申请百度网盘资源搜索入口
  • 哈尔滨做平台网站平台公司吗常见的关键词
  • 把网站做成app新闻稿发布平台
  • 仿网站模板如何快速搭建网站
  • 56物流网深圳谷歌seo推广
  • 邹平做网站公司seo快速排名站外流量推广
  • 企业做的网站推广方案的步骤郑州疫情最新动态
  • 知名网站制作公企业网站运营推广
  • 一级a做爰电影片免费网站海淀区seo搜索引擎
  • 教学网站开发源码windows优化大师好不好
  • 做网站需要知道哪些事情小程序推广赚佣金平台
  • 一级造价师注册查询系统平台入口济南seo顾问
  • 深圳购物网站建设公司建站小程序
  • wordpress图片分享插件下载地址星乐seo网站关键词排名优化
  • 做网站要什么知识seo排名培训学校
  • 义乌专业做网站做百度推广的网络公司
  • 常州市网站优化春哥seo博客
  • 网站开发常用js函数东莞新闻最新消息今天
  • WordPress的分類顯示插件seo综合查询中的具体内容有哪些
  • 百度糯米做网站多少钱百度云网盘入口
  • 大网站整站备份优化大师哪个好
  • php网站建设填空题a5站长网网站交易
  • 长沙市网站制作多少钱app软件开发