异常处理
通常程序出现错误,前台页面会显示升级之类的字眼,而后台会将错误给到日志文件中,本篇文章就会演示此操作
一、单个类中的异常处理
1、异常时页面需要显示的内容
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
对不起,系统升级中!!!
</body>
</html>
2、单个类中异常处理(int i = 2/0 — 算数异常)
需注解:@ExceptionHandler(Exception.class)
package com.java.demo1.controller;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.java.demo1.domain.User;
//普通类被用Controller注释就变成了servlet
@Controller
@RequestMapping(value="/aa")
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo1 {
//使用集合带
@RequestMapping(value = "/login10.action")
public String login10(Model model) {
User user1 = new User();
user1.setUsername("张三");
user1.setPassword("123456");
user1.setHobby("吃,睡");
try {
//字符串转时间
String birthday = "2021/2/12";
Date date = new SimpleDateFormat("yyyy/MM/dd").parse(birthday);
user1.setBirthday(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//运行到这里会报错
int i = 2/0;
User user2 = new User();
user2.setUsername("李四");
user2.setPassword("78910");
user2.setHobby("老,大");
try {
//字符串转时间
String birthday = "2021/2/16";
Date date = new SimpleDateFormat("yyyy/MM/dd").parse(birthday);
user2.setBirthday(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//集合
List<User> list = new ArrayList<User>();
Collections.addAll(list, user1,user2);
model.addAttribute("list", list);
return "success";
}
//单个类中异常处理方法(作用于当前类里面)
@ExceptionHandler(Exception.class)
public String myException(Exception e) {
Date date = new Date();
String ex = date+"\r\n"+e;
OutputStream out;
try {
out = new FileOutputStream("C:"+java.io.File.separator+"Users"+java.io.File.separator+"LUO"+java.io.File.separator+"LOG"+java.io.File.separator+"springmvc");
out.write(ex.getBytes());
out.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return "error";
}
}
二、统一异常处理
所有异常都会交给它来统一处理
1、建立了一个专门的异常处理类(通过 @ControllerAdvice 注解)
package com.java.exception;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalException {
@ExceptionHandler(Exception.class)
public String myException(Exception e) {
Date date = new Date();
String ex = date+"\r\n"+e+"\r\n";
OutputStream out;
try {
//最后一位参数为true,意思是待会儿往记事本中追加数据,而不是覆盖
out = new FileOutputStream("C:"+java.io.File.separator+"Users"+java.io.File.separator+"LUO"+java.io.File.separator+"LOG"+java.io.File.separator+"springmvc",true);
out.write(ex.getBytes());
out.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return "error";
}
}
2、我这里还需要增加一个扫描范围
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 在本配置文件中不要扫描service层和dao层 -->
<context:component-scan base-package="com.java.demo1.controller,com.java.exception"></context:component-scan>
<!-- 扫描springmvc中的注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器(只对转发有效,重定向无效) -->
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
现在再次访问login10.action,也会由这个专门处理异常的类来写入到日志中