JAVA出現異常跳轉到 異常頁面 案例

try{ } catch{ } 結構,

try{ } 中進行判斷userId是否爲空,並 throw 異常,

catch{ } 獲取異常並 mv.setViewName("itsm/weixin/error"); 跳轉到異常頁面。

	@RequestMapping(value = "/goWvehicleapplyAdd")
	public ModelAndView goWvehicleapplyAdd(HttpServletRequest request) throws Exception {
		try {
			String userId = getCurrentUserId();
			if(StringUtils.isEmpty(userId)) {
				throw new NullPointerException("[用車申請]獲取當前用戶失敗");
			}
			ModelAndView mv = this.getModelAndView();
			PageData pd = this.getPageData();
			pd.put("STAFF_ID", userId);
			List<PageData> staffInfoList = staffService.listDepartmentEmpByEmpIdInfo(pd);
			mv.setViewName("cms/vehicleweixin/vehicleapply/wvehicleapply_add");
			mv.addObject("msg", "vehicleapplysave");
			mv.addObject("pd", pd);
			mv.addObject("staffInfoList", staffInfoList);
			return mv;
		}catch(NullPointerException e) {
			LOGGER.error("[用車申請] 新增請求時發生異常", e);
			ModelAndView mv = this.getModelAndView();
			mv.setViewName("itsm/weixin/error");
			mv.addObject("msg", "未獲得當前登錄用戶信息,請重新登錄或聯繫管理員");
			return mv;
		}
	}

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title>系統異常</title>
<%@include file="weixin_head.jspf" %>
</head>

<body ontouchstart>

	<div class="weui-msg">
		<div class="weui-msg__icon-area">
			<i class="weui-icon-warn weui-icon_msg"></i>
		</div>
		<div class="weui-msg__text-area">
			<h2 class="weui-msg__title">系統異常,請聯繫管理員</h2>
			<p class="weui-msg__desc">${not empty msg ? msg : '未知異常!' }</p>
		</div>
	</div>
	<%@ include file="weixin_foot.jspf"%>
</body>
</html>

weixin_head.jspf

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
	content="width=device-width, initial-scale=1, user-scalable=no">

<meta name="description"
	content="澳洋IT共享平臺">

<link rel="stylesheet" href="static/weixin/example.css" />
<link rel="stylesheet" href="static/jquery-weui/weui.min.css" />
<link rel="stylesheet" href="static/jquery-weui/jquery-weui.min.css" />

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章