springmvc自定义异常信息

package com.itheima.exception;

public class CustomException extends Exception {

	// 定义异常的消息
	private String message;

	public CustomException(String message) {
		this.message = message;
	}

	// 获取异常信息
	public String getMessage() {
		return message;
	}
}

CustomException自定义异常类,通过有参构造函数保存错误信息

 

package com.itheima.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * 自定义异常处理器
 * @author ChonZ
 *
 */
public class CustomExceptionResolver implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {

		//打印异常信息
		ex.printStackTrace();
		
		//定义一个自己的异常类对象
		CustomException customException = null;
		
		//判断当前传入的异常对象是否是自定义异常对象
		if(ex instanceof CustomException) {
			customException = (CustomException)ex;
		} else {
			customException = new CustomException("系统问题,请联系管理员");
		}
		
		//创建ModelAndView对象
		ModelAndView mv = new ModelAndView();
		mv.setViewName("error");
		mv.addObject("errorMessage", customException.getMessage());
		return mv;
	}
}

CustomExceptionResolver自定义异常信息处理器,通过重写的方法,判断异常是否为自定义异常,是就直接使用异常信息,不是就按照指定的统一异常信息,通过ModelAndView绑定错误信息到目标页面去.

 

package com.itheima.web.controller;

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

import com.itheima.exception.CustomException;

/**
 * springmvc的异常处理
 * @author ChonZ
 *
 */
@Controller("exceptionController")
@RequestMapping("/springmvc")
public class ExceptionController {
	
	/**
	 * 测试自定义异常处理
	 * @param username
	 * @return
	 * @throws CustomException 
	 */
	@RequestMapping("/testException")
	public String testException(String username) throws CustomException {
		System.out.println("testException方法执行了..." + username);
		//判断用户名是否为空,如果为空的话,抛出异常
		if(StringUtils.isEmpty(username)) {
			throw new CustomException("用户名为空");
		}
		return "success";
	}	
}

ExceptionController 错误信息Controller类,用于模拟异常产生

 



		
		
		
		
		
			
			
		
		
		
		
		
		
		
		
		
		
		
		

pringmvc.xml配置文件

 



	day02_springmvc5_01response


	
	
		dispatcherServlet
		org.springframework.web.servlet.DispatcherServlet

		
		
			contextConfigLocation
			classpath:springmvc.xml
		
		1
	
	
	
	
		dispatcherServlet
		/
	
	
	
	
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
		
			forceEncoding
			true
		
	
	
		CharacterEncodingFilter
		/*
	




	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	

web.xml配置文件

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




springmvc的异常处理


	
	
用户名称:

index.jsp测试页面

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




错误提示页面


执行失败:${errorMessage}

error.jsp错误信息显示页面

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




成功页面


执行成功

成功页面

你可能感兴趣的:(Spingmvc)