国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

SpringMVC處理異常

2019-11-11 01:59:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

SPRingMVC處理異常

@(SpringMVC)[springmvc, 異常]

SpringMVC處理異常SpringMVC單異常處理SpitterController2SpittleNotFoundExceptionMyErrorspringMvc架構(gòu)級(jí)別異常處理案例自定義異常類(lèi)自定義全局異常處理器錯(cuò)誤頁(yè)面errorjsp在SpringMVC配置文件中配置創(chuàng)建異常測(cè)試

SpringMVC單異常處理

SpitterController2

package spittr.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.*;import spittr.Spittle;import spittr.data.SpitterRepository;// 使用RestController,相當(dāng)于在每個(gè)方法上加上了@ResponseBody@RestController@RequestMapping("/spitter2")public class SpitterController2 { private SpitterRepository spitterRepository; @Autowired public SpitterController2(SpitterRepository spitterRepository) { this.spitterRepository = spitterRepository; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Spittle spittleById(@PathVariable long id) { Spittle spittle = spitterRepository.findOne(id); if (spittle == null) { throw new SpittleNotFoundException(id); } return spittle; } @ExceptionHandler(SpittleNotFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public MyError spittleNotFound(SpittleNotFoundException e) { long spittleId = e.getSpittleId(); return new MyError(4, "Spittle[" + spittleId + "] not found"); }}

SpittleNotFoundException

package spittr.web;/** * Created by Switch on 2017/1/14. */public class SpittleNotFoundException extends RuntimeException { private long spittleId; public SpittleNotFoundException(long spittleId) { this.spittleId = spittleId; } public long getSpittleId() { return spittleId; }}

MyError

package spittr.web;/** * Created by Switch on 2017/1/14. */public class MyError { private int code; private String message; public MyError(int code, String message) { this.code = code; this.message = message; } public int getCode() { return code; } public String getMessage() { return message; }}

springMvc架構(gòu)級(jí)別異常處理

系統(tǒng)中異常包括兩類(lèi):預(yù)期異常和運(yùn)行時(shí)異常RuntimeException,前者通過(guò)捕獲異常從而獲取異常信息,后者主要通過(guò)規(guī)范代碼開(kāi)發(fā)、測(cè)試通過(guò)手段減少運(yùn)行時(shí)異常的發(fā)生。 系統(tǒng)的dao、service、controller 出現(xiàn)都通過(guò)throws Exception 向上拋出,最后由springmvc 前端控制器交由異常處理器進(jìn)行異常處理,如下圖: SpringMVC異常處理

案例

自定義異常類(lèi)

package com.pc.ssm.exception;/** * 自定義異常 * * @author Switch * @data 2017年1月13日 * @version V1.0 */public class CustomException extends Exception { private static final long serialVersionUID = -665787561868437767L; // 異常消息 private String message; public CustomException() { } public CustomException(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }}

自定義全局異常處理器

package com.pc.ssm.exception;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;/** * 自定義全局異常處理器 * * @author Switch * @data 2017年1月13日 * @version V1.0 */public class CustomGlobalExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ex.printStackTrace(); String msg = ""; if (ex instanceof CustomException) { msg = ((CustomException) ex).getMessage(); } else { msg = "系統(tǒng)繁忙,請(qǐng)稍后再試,或與管理員取得聯(lián)系!"; } ModelAndView modelAndView = new ModelAndView(); // 添加錯(cuò)誤信息 modelAndView.addObject("error", msg); modelAndView.setViewName("error"); return modelAndView; }}

錯(cuò)誤頁(yè)面error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>系統(tǒng)通知</title> </head> <body> <h2>${error}</h2> </body></html>

在SpringMVC配置文件中配置

<!-- 配置異常處理器 --> <bean id="handlerExceptionResolver" class="com.pc.ssm.exception.CustomGlobalExceptionResolver"/>

創(chuàng)建異常

@RequestMapping("/showEdit") public String showEdit(@RequestParam(value = "id") String id, Model model) throws Exception { User user = userService.findById(Integer.parseInt(id)); // 創(chuàng)造個(gè)異常 if(user == null) { throw new CustomException("用戶(hù)不存在!"); } else if(user.getId() == 1) { int i = 1 / 0; } model.addAttribute("user", user); return "edit"; }

測(cè)試

訪(fǎng)問(wèn):http://localhost:8080/SpringMVCAdvanced/user/showEdit.action?id=1,出現(xiàn)“系統(tǒng)繁忙,請(qǐng)稍后再試,或與管理員取得聯(lián)系!”。 訪(fǎng)問(wèn):http://localhost:8080/SpringMVCAdvanced/user/showEdit.action?id=2,正常訪(fǎng)問(wèn)。 訪(fǎng)問(wèn):http://localhost:8080/SpringMVCAdvanced/user/showEdit.action?id=1000023,出現(xiàn)“用戶(hù)不存在!”。


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜宾县| 南皮县| 抚顺市| 绵竹市| 苏州市| 赣榆县| 夏邑县| 九寨沟县| 梁平县| 那曲县| 威海市| 白沙| 榆社县| 漳州市| 昌邑市| 陵水| 棋牌| 军事| 定州市| 微博| 莆田市| 方山县| 平陆县| 铜陵市| 繁峙县| 汨罗市| 台东市| 孙吴县| 桐庐县| 塘沽区| 南宫市| 安乡县| 洪湖市| 吉安县| 呼玛县| 澜沧| 兴和县| 吴江市| 安陆市| 黔西| 扶绥县|