日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC异常处理之异常处理代码编写

發(fā)布時間:2024/4/13 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC异常处理之异常处理代码编写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 開啟注解掃描 --><context:component-scan base-package="com.learn"/><!-- 視圖解析器對象 --><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"/><property name="suffix" value=".jsp"/></bean><!--配置異常處理器--><bean id="sysExceptionResolver" class="com.learn.exception.SysExceptionResolver"/><!-- 開啟SpringMVC框架注解的支持 --><mvc:annotation-driven /></beans> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><h3>異常處理</h3><a href="user/testException" >異常處理</a></body> </html> package com.learn.controller;import com.learn.exception.SysException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller @RequestMapping("/user") public class UserController {@RequestMapping("/testException")public String testException() throws SysException{System.out.println("testException執(zhí)行了...");try {// 模擬異常int a = 10/0;} catch (Exception e) {// 打印異常信息e.printStackTrace();// 拋出自定義異常信息throw new SysException("查詢所有用戶出現錯誤了...");}return "success";}} package com.learn.exception;/*** 自定義異常類*/ public class SysException extends Exception{// 存儲提示信息的private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public SysException(String message) {this.message = message;}} package com.learn.exception;import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 異常處理器*/ public class SysExceptionResolver implements HandlerExceptionResolver{/*** 處理異常業(yè)務邏輯* @param request* @param response* @param handler* @param ex* @return*/public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {// 獲取到異常對象SysException e = null;if(ex instanceof SysException){e = (SysException)ex;}else{e = new SysException("系統正在維護....");}// 創(chuàng)建ModelAndView對象ModelAndView mv = new ModelAndView();mv.addObject("errorMsg",e.getMessage());mv.setViewName("error");return mv;}} <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head><title>Title</title> </head> <body>${errorMsg}</body> </html>

?

總結

以上是生活随笔為你收集整理的SpringMVC异常处理之异常处理代码编写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。