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

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

SpringMVC處理異常

2019-11-11 01:58:03
字體:
來源:轉載
供稿:網友

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,出現“用戶不存在!”。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新绛县| 珲春市| 恭城| 万荣县| 绍兴市| 正镶白旗| 林芝县| 五常市| 安阳县| 三门峡市| 象州县| 临朐县| 广东省| 新郑市| 东阿县| 兴隆县| 麟游县| 凌海市| 桃园市| 昌邑市| 新闻| 庆元县| 祥云县| 忻州市| 柳林县| 京山县| 澄城县| 阿拉尔市| 调兵山市| 高唐县| 方山县| 左贡县| 玛纳斯县| 平罗县| 凭祥市| 隆昌县| 盐山县| 海原县| 稷山县| 安顺市| 重庆市|