zoukankan      html  css  js  c++  java
  • 使用LoggerFactory输出异常信息到页面

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.exception</groupId>
        <artifactId>Exception</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <!-- 声明项目配置依赖编码格式为 utf-8 -->
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <fastjson.version>1.2.24</fastjson.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>3.3.1</version>
                
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    package com.tszr.exception.exceptionh;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class ExceptionController {
        protected static final Logger log = LoggerFactory.getLogger(ExceptionController.class);
        
        @RequestMapping("/exceptionMethod")
        public String exceptionMethod(Model model) throws Exception {
            model.addAttribute("msg", "没有抛出异常");
            int num = 1 / 0;
            log.info(String.valueOf(num));
            return "index";
        }
        
        /**
         * 描述:捕获 ExceptionController 中的 ArithmeticException 异常
         * @param model 将Model对象注入到方法中
         * @param e 将产生异常对象注入到方法中
         * @return 指定错误页面
         */
        @ExceptionHandler(value = {ArithmeticException.class})
        public String arithmeticExceptionHandle(Model model, Exception e) {
            model.addAttribute("msg", "除数不能是0哦");
            model.addAttribute("msgg","@ExceptionHandler" + e.getMessage() + "除数不能是0哦");        
            System.out.println("****************************");
            System.out.println("****************************");
            System.out.println(e.getMessage());
            System.out.println("****************************");
            System.out.println("****************************");
            System.out.println(model.toString());
            log.info(e.getMessage());
            return "error";
        }
    
    }
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>自定义 springboot 异常处理页面</title>
    </head>
    <body>
        Springboot BasicExceptionController 错误页面
        <br>
        <span th:text="${msg}"></span>
        <br>
        <span th:text="${msgg}"></span>
    </body>
    </html>
    <!DOCTYPE html>
    <!--引入命名空间-->
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script th:inline="javascript">
            var name= [[${name}]]
            console.log(name);
        </script>
    </head>
    <body>
        <table border="1" width="60%" align="center">
            <tr>
                <td>编号</td>
                <td>姓名</td>
                <td>地址</td>
            </tr>
            <tr th:each="user:${list}">
                <td th:text="${user.id}"></td>
                <td th:text="${user.name}"></td>
                <td th:text="${user.address}"></td>
            </tr>
        </table>
    </body>
    </html>
    package com.tszr.exception;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ExceptionApplication {
        public static void main(String[] args) {
            SpringApplication.run(ExceptionApplication.class, args);
        }
    }

  • 相关阅读:
    VMware 虚拟机扩容磁盘
    记录一次Jenkins多分支构建问题
    ceph 集群快速部署
    阿里云EMAS发布套餐订阅云服务
    我研究过的OA产品这是简单的总结
    Hello,OA!Hello,工作流!寻找OA和工作流的旅途记录
    疑难杂症1-去掉网站里的特殊编码&#65279
    IIS 平台NET无后缀名伪静态实现办法
    让人无语的面试题!!排序!你试试?
    优秀的大企业报告辅助撰写系统介绍
  • 原文地址:https://www.cnblogs.com/tszr/p/15443025.html
Copyright © 2011-2022 走看看