Java代码
- @Repository("testDao")
- public class TestDao {
- public void exception(Integer id) throws Exception {
- switch(id) {
- case 1:
- throw new BusinessException("12", "dao12");
- case 2:
- throw new BusinessException("22", "dao22");
- case 3:
- throw new BusinessException("32", "dao32");
- case 4:
- throw new BusinessException("42", "dao42");
- case 5:
- throw new BusinessException("52", "dao52");
- default:
- throw new ParameterException("Dao Parameter Error");
- }
- }
- }
Java代码
- public interface TestService {
- public void exception(Integer id) throws Exception;
- public void dao(Integer id) throws Exception;
- }
- ("testService")
- public class TestServiceImpl implements TestService {
- private TestDao testDao;
- public void exception(Integer id) throws Exception {
- switch(id) {
- case 1:
- throw new BusinessException("11", "service11");
- case 2:
- throw new BusinessException("21", "service21");
- case 3:
- throw new BusinessException("31", "service31");
- case 4:
- throw new BusinessException("41", "service41");
- case 5:
- throw new BusinessException("51", "service51");
- default:
- throw new ParameterException("Service Parameter Error");
- }
- }
- @Override
- public void dao(Integer id) throws Exception {
- testDao.exception(id);
- }
- }
Java代码
- @Controller
- public class TestController {
- @Resource
- private TestService testService;
- @RequestMapping(value = "/controller.do", method = RequestMethod.GET)
- public void controller(HttpServletResponse response, Integer id) throws Exception {
- switch(id) {
- case 1:
- throw new BusinessException("10", "controller10");
- case 2:
- throw new BusinessException("20", "controller20");
- case 3:
- throw new BusinessException("30", "controller30");
- case 4:
- throw new BusinessException("40", "controller40");
- case 5:
- throw new BusinessException("50", "controller50");
- default:
- throw new ParameterException("Controller Parameter Error");
- }
- }
- @RequestMapping(value = "/service.do", method = RequestMethod.GET)
- public void service(HttpServletResponse response, Integer id) throws Exception {
- testService.exception(id);
- }
- @RequestMapping(value = "/dao.do", method = RequestMethod.GET)
- public void dao(HttpServletResponse response, Integer id) throws Exception {
- testService.dao(id);
- }
- }
Java代码
- <%@ page contentType="text/html; charset=UTF-8"%>
- <html>
- <head>
- <title>Maven Demo</title>
- </head>
- <body>
- <h1>所有的演示例子</h1>
- <h3>[url=./dao.do?id=1]Dao正常错误[/url]</h3>
- <h3>[url=./dao.do?id=10]Dao参数错误[/url]</h3>
- <h3>[url=./dao.do?id=]Dao未知错误[/url]</h3>
- <h3>[url=./service.do?id=1]Service正常错误[/url]</h3>
- <h3>[url=./service.do?id=10]Service参数错误[/url]</h3>
- <h3>[url=./service.do?id=]Service未知错误[/url]</h3>
- <h3>[url=./controller.do?id=1]Controller正常错误[/url]</h3>
- <h3>[url=./controller.do?id=10]Controller参数错误[/url]</h3>
- <h3>[url=./controller.do?id=]Controller未知错误[/url]</h3>
- <h3>[url=./404.do?id=1]404错误[/url]</h3>
- </body>
- </html>
Xml代码
- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
- <!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->
- <property name="defaultErrorView" value="error"></property>
- <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
- <property name="exceptionAttribute" value="ex"></property>
- <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 -->
- <property name="exceptionMappings">
- <props>
- <prop key="cn.basttg.core.exception.BusinessException">error-business</prop>
- <prop key="cn.basttg.core.exception.ParameterException">error-parameter</prop>
- <!-- 这里还可以继续扩展对不同异常类型的处理 -->
- </props>
- </property>
- </bean>
Java代码
- public class MyExceptionHandler implements HandlerExceptionResolver {
- public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
- Exception ex) {
- Map<String, Object> model = new HashMap<String, Object>();
- model.put("ex", ex);
- // 根据不同错误转向不同页面
- if(ex instanceof BusinessException) {
- return new ModelAndView("error-business", model);
- }else if(ex instanceof ParameterException) {
- return new ModelAndView("error-parameter", model);
- } else {
- return new ModelAndView("error", model);
- }
- }
- }
Xml代码
- <bean id="exceptionHandler" class="cn.basttg.core.exception.MyExceptionHandler"/>
Java代码
- public class BaseController {
- /** 基于@ExceptionHandler异常处理 */
- @ExceptionHandler
- public String exp(HttpServletRequest request, Exception ex) {
- request.setAttribute("ex", ex);
- // 根据不同错误转向不同页面
- if(ex instanceof BusinessException) {
- return "error-business";
- }else if(ex instanceof ParameterException) {
- return "error-parameter";
- } else {
- return "error";
- }
- }
- }
Java代码
- public class TestController extends BaseController
Xml代码
- <!-- 出错页面定义 -->
- <error-page>
- <exception-type>java.lang.Throwable</exception-type>
- <location>/500.jsp</location>
- </error-page>
- <error-page>
- <error-code>500</error-code>
- <location>/500.jsp</location>
- </error-page>
- <error-page>
- <error-code>404</error-code>
- <location>/404.jsp</location>
- </error-page>
- <!-- 这里可继续增加服务器错误号的处理及对应显示的页面 -->