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

首頁 > 學院 > 開發設計 > 正文

SpringMVC處理異常

2019-11-11 03:10:48
字體:
來源:轉載
供稿:網友

SPRingMVC處理異常

@(SpringMVC)[springmvc, 異常]

SpringMVC處理異常SpringMVC單異常處理SpitterController2SpittleNotFoundExceptionMyErrorspringMvc架構級別異常處理案例自定義異常類自定義全局異常處理器錯誤頁面errorjsp在SpringMVC配置文件中配置創建異常測試

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,相當于在每個方法上加上了@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架構級別異常處理

系統中異常包括兩類:預期異常和運行時異常RuntimeException,前者通過捕獲異常從而獲取異常信息,后者主要通過規范代碼開發、測試通過手段減少運行時異常的發生。 系統的dao、service、controller 出現都通過throws Exception 向上拋出,最后由springmvc 前端控制器交由異常處理器進行異常處理,如下圖: SpringMVC異常處理

案例

自定義異常類

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 = "系統繁忙,請稍后再試,或與管理員取得聯系!"; } ModelAndView modelAndView = new ModelAndView(); // 添加錯誤信息 modelAndView.addObject("error", msg); modelAndView.setViewName("error"); return modelAndView; }}

錯誤頁面error.jsp

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

在SpringMVC配置文件中配置

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

創建異常

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

測試

訪問:http://localhost:8080/SpringMVCAdvanced/user/showEdit.action?id=1,出現“系統繁忙,請稍后再試,或與管理員取得聯系!”。 訪問:http://localhost:8080/SpringMVCAdvanced/user/showEdit.action?id=2,正常訪問。 訪問:http://localhost:8080/SpringMVCAdvanced/user/showEdit.action?id=1000023,出現“用戶不存在!”。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青冈县| 富平县| 溧阳市| 理塘县| 高邮市| 广平县| 赫章县| 临桂县| 揭东县| 邯郸县| 阿合奇县| 阳高县| 什邡市| 崇义县| 于田县| 衡阳市| 富阳市| 凤翔县| 芦溪县| 黄龙县| 新巴尔虎左旗| 通辽市| 阳江市| 古丈县| 黄梅县| 汉源县| 丹寨县| 乳山市| 资中县| 红安县| 清徐县| 彭州市| 东乌珠穆沁旗| 佳木斯市| 仁寿县| 五原县| 肇源县| 龙海市| 绍兴市| 扎鲁特旗| 肃北|