CustomExceptionHandler 생성
// 모든 에러 잡아내는 어노테이션 @ControllerAdvice // 응답 에러 컨트롤러 (view==파일 리턴) public class CustomExceptionHandler { @ExceptionHandler(Exception.class) public @ResponseBody String error1(Exception e){ return Script.back(e.getMessage()); } }
Exception 부분 수정
원래 return null;
Exception이 터지면서 ExceptionHandler 실행
null이 이제 return 되지 않으므로 코드 수정
Script 코드
public class Script { public static String back(String msg) { StringBuilder sb = new StringBuilder(); sb.append("<script>"); sb.append("alert('"+msg+"');"); sb.append("history.back();"); sb.append("</script>"); return sb.toString(); } public static String href(String path) { StringBuilder sb = new StringBuilder(); sb.append("<script>"); sb.append("location.href='"+path+"';"); sb.append("</script>"); return sb.toString(); } public static String href(String path, String msg) { StringBuilder sb = new StringBuilder(); sb.append("<script>"); sb.append("alert('"+msg+"');"); sb.append("location.href='"+path+"';"); sb.append("</script>"); return sb.toString(); } }
Share article