017_SpringBoot异常处理方式-自定义错误页面

1. 使用maven构建SpringBoot的名叫spring-boot-exception1项目

2. pom.xml 

<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.bjbs</groupId>
	<artifactId>spring-boot-exception1</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.13.RELEASE</version>
	</parent>

	<!-- 修改jdk版本 -->
	<properties>
		<java.version>1.8</java.version>
		<!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html标签不规范报错 -->
		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
	</properties>

	<dependencies>
		<!-- springBoot的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
</project>

3. 新建UserController.java

package com.bjbs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * SpringBoot处理异常方式一: 自定义错误页面
 */
@Controller
public class UserController {
	@RequestMapping("/nullException")
	public String nullException(){
		String str = null;
		str.length();
		return "index";
	}
	
	@RequestMapping("/byZeroException")
	public String byZeroException(){
		int a = 10/0;
		return "index";
	}
	
	@RequestMapping("/index")
	public String indexhtml(){
		return "index";
	}
}

4. 新建App.java

package com.bjbs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * springBoot启动类
 */
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

5. SpringBoot默认的处理异常的机制: SpringBoot默认的已经提供了一套处理异常的机制。一旦程序中出现了异常SpringBoot会像/error的url发送请求。在SpringBoot中提供了一个叫BasicExceptionController来处理/error请求, 然后跳转到默认显示异常的页面来展示异常信息。

6. 运行项目, 使用浏览器访问空指针异常

7. 在src/main/resources/templates下, 新建index.html 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>主页</title>
	</head>
	<body>
		<span th:text="Hello"></span>
	</body>
</html>

8. 在src/main/resources/templates下, 新建error.html, 注意: 名称必须叫error。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>错误提示页面</title>
	</head>
	<body>
		出错了, 请与管理员联系。
		<span th:text="${exception}"></span>
	</body>
</html>

9. 运行项目, 使用浏览器访问空指针异常

10. 运行项目, 使用浏览器访问除数为0异常 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值