注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CsjzCheck {
String value();
}
注解功能类,校验抛出异常
@Aspect
@Component
public class CsjzCheckAspect extends BaseController {
@Resource
YkCsjzMapper ykCsjzMapper;
public static final Logger logger = LoggerFactory.getLogger(CsjzCheckAspect.class);
@Pointcut("@annotation(com.zdww.chis.common.csjz.CsjzCheck)")
public void location() {
}
@Before("location() && @annotation(csjzCheck)")
public void action(CsjzCheck csjzCheck) {
String kflb = csjzCheck.value();
if (Constants.KSSZ_KFLB_YK.equals(kflb)) {
if ("0".equals(ykCsjzMapper.getJzbz(getOrgCode(), getYkdm(), kflb))) {
throw new PageOutException("当前药库未建账!");
}
} else if (Constants.KSSZ_KFLB_YF.equals(kflb)) {
if ("0".equals(ykCsjzMapper.getJzbz(getOrgCode(), getYfdm(), kflb))) {
throw new PageOutException("当前药房未建账!");
}
} else {
if ("0".equals(ykCsjzMapper.getJzbz(getOrgCode(), getYfdm(), kflb))) {
throw new PageOutException("当前库房未建账!");
}
}
}
}
异常类
public class PageOutException extends NullPointerException{
public PageOutException(String s) {
super(s);
}
}
异常拦截重定向类
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = PageOutException.class)
public ModelAndView pageOutExceptionHandler(HttpServletRequest req, PageOutException e){
ModelAndView mv=new ModelAndView("error/pageOut");
System.out.println(e.getMessage());
mv.addObject("errMsg",e.getMessage());
return mv;
}
}
前端页面
<!DOCTYPE html>
<html class="his-html" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
</head>
<body class="his-body">
</div>
<script type="text/javascript" th:inline="javascript">
var msgAlter = $.messager.alert({
title: '提示',
msg: '<div style=" text-align: center;padding-top:6px;font-size:14px;color:#333333;">' + [[${errMsg}]] + '</div>',
icon: 'cusError'
});
msgAlter.window({
modal: true, onBeforeClose: function () {
$('#his_main_tab').tabs('close', $('#his_main_tab').tabs('getTabIndex', $('#his_main_tab').tabs('getSelected')))
}
});
</script>
</body>
</html>