编写错误页面,异常页面,当项目发布出现了异常和错误,你肯定不想给客户看到那些错误码,严重影响体验。
Jar包:
控制层MyController:
package com.zsl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping("/userA")
public ModelAndView userA(Integer num) {
// TODO Auto-generated method stub
System.out.println("进入了userA");
if (num == 1) {
throw new NullPointerException();
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user");
return modelAndView;
}
@RequestMapping("/userB")
public ModelAndView userB(Integer num) {
// TODO Auto-generated method stub
System.out.println("进入了userB");
if (num == 2) {
throw new ArithmeticException();
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user");
return modelAndView;
}
}
异常处理类:
package com.zsl.exception;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice
@Component
public class MyException {
@ExceptionHandler(value=NullPointerException.class)
public ModelAndView exceptionA() {
// TODO Auto-generated method stub
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", "空指针异常");
modelAndView.setViewName("error");
return modelAndView;
}
@ExceptionHandler(value=ArithmeticException.class)
public ModelAndView exceptionB() {
// TODO Auto-generated method stub
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", "算术异常");
modelAndView.setViewName("error");
return modelAndView;
}
}
异常处理类还可以这么配置,不加注解但是需要实现HandlerExceptionResolver接口:
package com.zsl.exception;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
@Component
public class MyException implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
Exception arg3) {
// TODO Auto-generated method stub
ModelAndView modelAndView = new ModelAndView();
if (arg3 instanceof NullPointerException) {
modelAndView.addObject("msg", "空指针异常");
} else if(arg3 instanceof ArithmeticException){
modelAndView.addObject("msg", "算术异常");
}else {
modelAndView.addObject("msg", "其他异常");
}
modelAndView.setViewName("error");
return modelAndView;
}
}
Pojo:
package com.zsl.pojo;
public class User {
private String name;
private String sex;
private Integer Age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return Age;
}
public void setAge(Integer age) {
Age = age;
}
public User(String name, String sex, Integer age) {
super();
this.name = name;
this.sex = sex;
Age = age;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [name=" + name + ", sex=" + sex + ", Age=" + Age + "]";
}
}
Springmvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.zsl.*"/>
<!-- 开启MVC注解 -->
<mvc:annotation-driven/>
<!-- 视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>