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" />