728x90

개요

  String 변수가 null이나 undefined인지 확인하는 기능은 쓰임새가 많다. Java라면 그런 목적으로 Apache commons의 StringUtils를 많이 사용하고, String 관련 메서드를 사용할 때 null 체크를 생략시켜줘서 편리하다. JavaScript에서도 클래스 및 메서드 명칭이 동일하고 동작구조도 최대한 동일하면 협업 시 팀원들이 빠르게 기능을 이해하고 사용할 수 있을 거란 생각이 들었다.

샘플코드

  Apache commons의 StringUtils의 메서드를 모두 구현하려면 시간이 많이 들어서 당장 자주 쓸 기능만 작성했다. 나머지는 필요해질 때마다 추가 작성하면 된다.

  [2023-03-28] isEmpty, isNotEmpty, isNotBlank 추가.

<StringUtils.js>

/**
 * Apache commons의 StringUtils 클래스의 편리한 기능을 자바스크립트에서 그대로 사용하기 위한 객체. 
 */
const StringUtils = {};
/**
 * 
 * @param str {String} 확인할 문자열
 * @return 문자열 길이. 문자열이 없으면 0.
 */
StringUtils.length = function(str) {
	if (str) {
		return str.length;
	}
	
	return 0;
};

/**
 * <p>Checks if a String is empty ("") or null or undefined.</p>
 *
 * <pre>
 * StringUtils.isEmpty(undefined) = true
 * StringUtils.isEmpty(null)      = true
 * StringUtils.isEmpty("")        = true
 * StringUtils.isEmpty(" ")       = false
 * StringUtils.isEmpty("bob")     = false
 * StringUtils.isEmpty("  bob  ") = false
 * </pre>
 * 
 * @param cs {String} 확인할 문자열
 * @return {boolean} <code>true</code> if the cs is empty or null or undefined.
 */
StringUtils.isEmpty = function(cs) {
	if (cs == undefined || cs == null) {
		return true;
	}
	
	if (!("string" === typeof(cs) || cs instanceof String)) {
		throw new TypeError("Argument cs must be type of String.");
	}
	
	return cs.length == 0;
};

/**
 * <p>Checks if a String is not empty ("") and not null and not undefined.</p>
 *
 * <pre>
 * StringUtils.isNotEmpty(undefined) = false
 * StringUtils.isNotEmpty(null)      = false
 * StringUtils.isNotEmpty("")        = false
 * StringUtils.isNotEmpty(" ")       = true
 * StringUtils.isNotEmpty("bob")     = true
 * StringUtils.isNotEmpty("  bob  ") = true
 * </pre>
 * 
 * @param cs {String} 확인할 문자열
 * @return {boolean} <code>true</code> if the cs is not empty and not null and not undefined.
 */
StringUtils.isNotEmpty = function(cs) {
	return !StringUtils.isEmpty(cs);
};

/**
 * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
 *
 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>
 * StringUtils.isBlank(undefined) = true
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 * 
 * @param cs {String} 확인할 문자열
 * @return {boolean} <code>true</code> if the cs is undefined, null, empty or whitespace only.
 */
StringUtils.isBlank = function(cs) {
	if (cs == undefined || cs == null) {
		return true;
	}

	if (!("string" === typeof(cs) || cs instanceof String)) {
		throw new TypeError("Argument cs must be type of String.");
	}

    let strLen;
    if ((strLen = cs.length) == 0) {
        return true;
    }
//    for (var i = 0; i < strLen; i++) {
//        if (!StringUtils.isWhitespace(cs.charAt(i))) {
//            return false;
//        }
//    }
        if (!StringUtils.isWhitespace(cs)) {
            return false;
        }
    return true;
};

/**
 * <pre>
 * StringUtils.isNotBlank(undefined) = false
 * StringUtils.isNotBlank(null)      = false
 * StringUtils.isNotBlank("")        = false
 * StringUtils.isNotBlank(" ")       = false
 * StringUtils.isNotBlank("bob")     = true
 * StringUtils.isNotBlank("  bob  ") = true
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return <code>true</code> if the CharSequence is
 *  not empty and not null and not whitespace only
 */
StringUtils.isNotBlank = function(cs) {
    return !StringUtils.isBlank(cs);
}

/**
 * Apache commons의 StringUtils 클래스에는 없지만, 유용할 듯하여 별도 메서드를 추가함.
 * @param cs {String} 확인할 문자열
 * @return {boolean} <code>true</code> if the str is undefined, null, empty or whitespace only.
 */
StringUtils.isWhitespace = function(cs) {
	if (!("string" === typeof(cs) || cs instanceof String)) {
		throw new TypeError("Argument cs must be type of String.");
	}
	
    if (/^\s+$/g.test(cs)) {
        return true;
    }
    return false;
};

/**
*
* <pre>
* StringUtils.trim(undefined)     = undefined
* StringUtils.trim(null)          = null
* StringUtils.trim("")            = ""
* StringUtils.trim("     ")       = ""
* StringUtils.trim("abc")         = "abc"
* StringUtils.trim("    abc    ") = "abc"
* </pre>
*
* @param str {String} the String to be trimmed, may be null
* @return {String} the trimmed string, <code>null</code> if null String input
*/
StringUtils.trim = function(str) {
	return (str == null || str == undefined) ? str : str.trim();
};
/**
 *
 * <pre>
 * StringUtils.trimToNull(undefined)     = null
 * StringUtils.trimToNull(null)          = null
 * StringUtils.trimToNull("")            = null
 * StringUtils.trimToNull("     ")       = null
 * StringUtils.trimToNull("abc")         = "abc"
 * StringUtils.trimToNull("    abc    ") = "abc"
 * </pre>
 *
 * @param str {String} the String to be trimmed, may be null
 * @return {String} the trimmed String,
 *  <code>null</code> if only chars &lt;= 32, empty or null String input
 */
StringUtils.trimToNull = function(str) {
	const ts = StringUtils.trim(str);
	return StringUtils.isEmpty(ts) ? null : ts;
};
/**
 *
 * <pre>
 * StringUtils.trimToEmpty(undefined)     = ""
 * StringUtils.trimToEmpty(null)          = ""
 * StringUtils.trimToEmpty("")            = ""
 * StringUtils.trimToEmpty("     ")       = ""
 * StringUtils.trimToEmpty("abc")         = "abc"
 * StringUtils.trimToEmpty("    abc    ") = "abc"
 * </pre>
 *
 * @param str {String} the String to be trimmed, may be null
 * @return {String} the trimmed String, or an empty String if <code>null</code> input
 */
StringUtils.trimToEmpty = function(str) {
	return (str == null || str == undefined) ? "" : str.trim();
};

 

단위테스트코드

<StringUtilsTest.html>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>StringUtilsTest</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.19.3.css">
</head>
<body>
	<div id="qunit"></div>
	<div id="qunit-fixture"></div>
	<script src="https://code.jquery.com/qunit/qunit-2.19.3.js"></script>
	<script src="./StringUtils.js"></script>
	<script>
		QUnit.module('isEmpty', function() {
			let undfnd;
			const nll = null;
			const empty = "";
			const blank = new String(" ");
			const longBlank = new String("      ");
			const notBlank = "asdf";
			QUnit.test('isEmpty', function(assert) {
				assert.true(StringUtils.isEmpty(undfnd), 'undefined');
				assert.true(StringUtils.isEmpty(nll), 'null');
				assert.true(StringUtils.isEmpty(empty), 'empty');
				assert.false(StringUtils.isEmpty(blank), 'blank');
				assert.false(StringUtils.isEmpty(longBlank), 'long blank');
				assert.false(StringUtils.isEmpty(notBlank));
			});
		});

		QUnit.module('isNotEmpty', function() {
			let undfnd;
			const nll = null;
			const empty = "";
			const blank = new String(" ");
			const longBlank = new String("      ");
			const notBlank = "asdf";
			QUnit.test('isNotEmpty', function(assert) {
				assert.false(StringUtils.isNotEmpty(undfnd), 'undefined');
				assert.false(StringUtils.isNotEmpty(nll), 'null');
				assert.false(StringUtils.isNotEmpty(empty), 'empty');
				assert.true(StringUtils.isNotEmpty(blank), 'blank');
				assert.true(StringUtils.isNotEmpty(longBlank), 'long blank');
				assert.true(StringUtils.isNotEmpty(notBlank));
			});
		});

		QUnit.module('isBlank', function() {
			let undfnd;
			const nll = null;
			const empty = "";
			const blank = new String(" ");
			const longBlank = new String("      ");
			const notBlank = "asdf";
			QUnit.test('isBlank', function(assert) {
				assert.true(StringUtils.isBlank(undfnd), 'undefined');
				assert.true(StringUtils.isBlank(nll), 'null');
				assert.true(StringUtils.isBlank(empty), 'empty');
				assert.true(StringUtils.isBlank(blank), 'blank');
				assert.true(StringUtils.isBlank(longBlank), 'long blank');
				assert.false(StringUtils.isBlank(notBlank));
			});
		});

		QUnit.module('isNotBlank', function() {
			let undfnd;
			const nll = null;
			const empty = "";
			const blank = new String(" ");
			const longBlank = new String("      ");
			const notBlank = "asdf";
			QUnit.test('isBlank', function(assert) {
				assert.false(StringUtils.isNotBlank(undfnd), 'undefined');
				assert.false(StringUtils.isNotBlank(nll), 'null');
				assert.false(StringUtils.isNotBlank(empty), 'empty');
				assert.false(StringUtils.isNotBlank(blank), 'blank');
				assert.false(StringUtils.isNotBlank(longBlank), 'long blank');
				assert.true(StringUtils.isNotBlank(notBlank));
			});
		});

		QUnit.module('trim', function() {
			QUnit.test('trim', function(assert) {
				assert.equal(StringUtils.trim(undefined), undefined);
				assert.equal(StringUtils.trim(null), null);
				assert.equal(StringUtils.trim(""), "");
				assert.equal(StringUtils.trim(" "), "");
				assert.equal(StringUtils.trim("    "), "");
				assert.equal(StringUtils.trim("asdf"), "asdf");
				assert.equal(StringUtils.trim("  asdf  "), "asdf");
			});
		});

		QUnit.module('trimToEmpty', function() {
			const blank = new String(" ");
			const longBlank = new String("      ");
			const notBlank = "asdf";
			QUnit.test('trimToEmpty', function(assert) {
				assert.equal(StringUtils.trimToEmpty(undefined), "");
				assert.equal(StringUtils.trimToEmpty(null), "");
				assert.equal(StringUtils.trimToEmpty(""), "");
				assert.equal(StringUtils.trimToEmpty("     "), "");
				assert.equal(StringUtils.trimToEmpty("abc"), "abc");
				assert.equal(StringUtils.trimToEmpty("    abc    "), "abc");
			});
		});
	</script>
</body>
</html>

단위테스트결과

 

반응형
728x90

  일정 시간 간격 또는 정해진 시간에 실행되도록 하는 기능이다.

기본예제코드

스케줄러 클래스.

클래스에 @Component를 달고 메서드에는 @Scheduled를 단다.

비동기로 실행되어야 하는 업무는 @Async를 단다.

@Component
public class ExampleScheduler {
	/**
	 * 생성자.
	 */
	public ExampleScheduler() {
		super();
	}

	/**
	 * 스케줄 테스트하기.
	 */
	@Scheduled(cron = "0/60 * * * * ?")
	public void testCron() {
		System.out.println("(cron = \"0/60 * * * * ?\") 시스템 시간을 기준으로 1분 마다 주기적으로 실행한다.");
	}

	/**
	 * 스케줄 테스트하기.
	 */
	@Scheduled(fixedRate = 8000)
	public void testFixedRate() {
		System.out.println("(fixedRate = 8000) 다른 스케줄 작업 완료 여부와 상관 없이 8000ms마다 실행한다.(단, 스케줄러풀 필요.)");
	}

	@Scheduled(fixedDelay = 2000)
	public void testFixedDelay() {
		final String prefix = DateFormatUtils.format(new Date(), "ddHHmmss");
		for (int i = 0; i < 10; i++) {
			System.out.println(prefix + "-" + i + ". 비동기가 제대로 작동하는지 확인하기 위해, 의도적으로 장시간 실행되도록 함.");
			try {
				Thread.sleep(1000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("(fixedDelay = 2000) 이 작업이 끝나면, 2000ms 뒤에 다시 실행한다.");
	}

	/**
	 * 비동기 스케줄 테스트하기.
	 */
	@Scheduled(fixedRate = 4000)
	@Async
	public void testFixedRateAysnc() {
		System.out.println("(fixedRate = 4000 @Async) 다른 스케줄 작업 완료 여부와 상관 없이 4000ms마다 실행한다.(단, 스케줄러풀 필요.)");
	}
}

“dispatcher-servlet.xml”에 설정들 추가.

  루트 beans에 task와 그 스키마 정보를 추가한다.

  그리고 task가 인식되도록 어노태이션에서 읽어들이도록 ‘<task:annotation-driven/>’를 추가한다.

<beans xmlns="http://www.springframework.org/schema/beans" …(생략)...
    xmlns:task="http://www.springframework.org/schema/task"
    …생략…
    xsi:schemaLocation="...
        http://www.springframework.org/schema/task     http://www.springframework.org/schema/task/spring-task.xsd
">

    <!-- 스케줄러 -@Scheduled가 있는 @Component 찾기 -->
    <task:annotation-driven scheduler="jobScheduler"/>
    <!-- 스케줄러 -@Async를 위한 스케줄러풀 설정 -->
    <task:scheduler id="jobScheduler" pool-size="10"/>

    …생략…
</beans>

  ‘<task:annotation-driven/>’의 스케줄러 컴포넌트 스캔 범위도 ‘<context:component-scan>’의 ‘base-package’에 설정한 스캔범위에 들어있어야 함은 동일하니 패키지 경로에 주의하자.

해설

  스케줄들은 실행시간이 되어도 다른 스케줄이 실행되는 중이면 끝날 때까지 대기하게 된다. 만약 반드시 정해진 시간에 실행되어야 하는 스케줄이라면 문제 될 것이다. 이를 위해 비동기로 실행시킬 수 있다.

  비동기는 스케줄 메서드에 @Async를 붙이면 된다. 단, 그냥 붙이기만 하면 실제로는 비동기로 실행이 되지 않는다. 비동기로 실행시키려면 [스케줄러 풀]이 필요하다.

참고 링크

반응형
728x90

  datagrid 내의 컬럼에 툴팁을 사용하고 싶을 경우 formatter를 이용해 정의하는 것을 생각해볼 수 있다. 그런데, formatter 내에서 tooltip 생성 코드를 바로 사용해도 생성이 되지 않는다. 아마도 datagrid 내의 formatter 내에서 구성한 엘리먼트가 formatter 코드가 모두 실행된 후에야 생성되기 때문일 것이다.( formatter 코드가 실행 완료되기 전에는 아직 존재하지 않는 엘리먼트여서 툴팁을 붙일 엘리먼트가 존재하지 않기 때문일 것으로 생각된다.)

  때문에 formatter 내에서는 툴팁을 띄울 이벤트에 대신 툴팁을 생성하는 함수를 연결해 놓는다. 이벤트가 발생할 때는 이미 요소가 있으므로 툴팁 생성이 가능하다.

  1. 툴팁에 노출할 데이터는 글로벌 변수에 저장한다.(datagrid를 그리면서 툴팁 생성까지 되지 않기 때문에 분리해서 실행이 되도록 데이터를 별도영역에 저장해 놓는다.)
  2. formatter를 이용해 툴팁이 필요한 컬럼의 엘리먼트에 고유한 아이디를 생성하고, 툴팁 생성 이벤트를 설정한다.
  3. 툴팁이 필요한 곳에 마우스이벤트가 발생하면, 툴팁을 생성한다.(현재로서는 엘리먼트에 EasyUI의 tooltip이 생성되었는지 따로 판단할 방법이 없다. 때문에 오류가 발생하면 생성되지 않은 것으로 판단하고 생성하도록 한다.)

<html>

	<table id="dg" title="Example" style="width:600px"  data-options="
            rownumbers:true,
            singleSelect:true,
            autoRowHeight:false,
            pagination:true,
            pageSize:10">
    <thead>
        <tr>
            <th data-options="field:'tupleId',width:100,align:'right'">Tuple ID</th>
            <th data-options="field:'name',width:100">Name</th>
            <th data-options="field:'files',width:200,formatter:uiService.fileFormatter">Files</th>
        </tr>
    </thead>

<JavaScript>

/**
 * 데이터.
 */
var gData = {};
gData.files = [];

/**
 * UI서비스.
 */
var uiService = {};
/**
 * 파일 정보 엘리먼트에 고유한 아이디를 생성하고, 툴팁 생성 이벤트를 설정한다.
 */
uiService.fileFormatter = function(value, row) {
	let result = null;

	gData.files[row.tupleId] = row.files;
	if (row.files) {
		const len = row.files.length;
		if(len > 0){
			const $tempElem = $('<div id="temp'+ row.tupleId + '"></div>');
			const fileListDiv = '<a href="javascript:void(0)" id="tipFile' + row.tupleId 
					+ '"onmouseover="uiService.buildFilesTooltip(\''+ row.tupleId + '\')"><font color="blue"><u>첨부파일 : ' + len + ' 건</u></font></a>';
			$tempElem.html(fileListDiv);

			result = $tempElem.html();
		}
	} else {
		result = '';
	}
	return result;
};

/**
 * 툴팁을 생성한다.
 */
uiService.buildFilesTooltip = function(tupleId) {
	try {
		const opts = $('#tipFile'+tupleId).tooltip('options');
		// EasyUI에 tooltip이 생성되어 있는지 여부를 판단할 방법이 지원되지 않기에, try~catch를 이용한다.
	} catch (e) {
		// tooltip이 없으면 오류가 발생하므로 생성한다.
		const files = gData.files[tupleId];
		var fileIds = [];
		var html = "";
		$.each(files, function(index, value) {
			html += '<div style="margin:5 5 10 5px;">';
			html += '<a href="javascript:void(0)" style="color:#c60;" onClick="fileService.downloadFile(\''
					+ value.fileId
					+ '\')"><u><b>'
					+ value['fileName'] + '</b></u></a>';
			html += '</div>';
			
			fileIds.push(value.fileId);

			if (index > 0 && (files.length - 1) == index) {
				html += '<div style="margin:5px;">'
					  + '<a href="javascript:void(0)" style="color:#c60;" '
					  + 'onClick="fileService.downloadAllFile([' + fileIds + '])"><u><b>일괄 다운로드</b></u></a></div>';
			}
		});
		const $tipFile = $('#tipFile'+ tupleId);
		$tipFile.tooltip({
			position : 'right',
			content : '<span style="color:#fff;">' + html + '</span>',
			onShow : function() {
				var t = $(this);
				t.tooltip('tip').unbind().bind('mouseenter', function() {
					t.tooltip('show');
				}).bind('mouseleave', function() {
					t.tooltip('hide');
				}).css({
					backgroundColor : '#d9d9d9',
					borderColor : '#d9d9d9',
					opacity : 1
				});
			}
		});
		$tipFile.tooltip('show');
	}
}

/**
 * 파일서비스.
 */
var fileService = {};
fileService.downloadFile = function(fileId) {
	// 예시이므로 실제로 파일을 다운로드하지는 않음.
	alert("fileId:" + fileId);
};
fileService.downloadAllFile = function(fileIds) {
	// 예시이므로 실제로 파일을 다운로드하지는 않음.
	alert("fileIds:" + fileIds);
};

/**
 * 초기화.
 */
$(document).ready(function() {
	const list = [
		{"tupleId":"2", "name": "홍길동", "files":[{"fileId":1, "fileName": "증명사진1"}]},
		{"tupleId":"4", "name": "홍길일", "files":[{"fileId":2, "fileName": "증명사진2"},{"fileId":3, "fileName": "여권사진"}]},
		{"tupleId":"8", "name": "홍길삼"},
		{"tupleId":"16", "name": "홍길사"}
	];
	$('#dg').datagrid().datagrid('loadData', list);
});
반응형
728x90

*getSelected : 현재 선택된 탭 가져오기.

var tab = $('#tt').tabs('getSelected');


*getTabIndex : 탭 인덱스 가져오기.

var tab = $('#tt').tabs('getSelected');
var index = $('#tt').tabs('getTabIndex',tab);
alert("index:" + index);

*id 확인하기.

const tab = $('#infoTabs').tabs('getSelected');
const tabOptions = tab.panel('options');
const infoTabId = tabOptions.id;
반응형
728x90

개요

Ajax 통신중에 프로그레스바 마스크를 호출하여 사용자의 조작을 막고 처리중임을 알릴 필요가 있다.

빠른 길잡이

jQuery UI 이용

  html로 프로그레스바를 준비한다.

  스타일

<style>
#progressbar {
	margin-top: 20px;
}

.progress-label {
	font-weight: bold;
	text-shadow: 1px 1px 0 #fff;
}

/* Dialog의 우상단 닫기버튼[x]를 숨기는 설정. */
.ui-dialog-titlebar-close {
	display: none;
}
</style>

  html

<!-- Progressbar Dialog -->
<div id="dialog" title="Progressbar Example" style="display: none;">
  <div class="progress-label">Starting...</div>
  <div id="progressbar"></div>
</div>

beforeSend에서 jQuery UI 프로그레스바를 노출시킨한다.

(*[2022-11-14] modal 속성이 누락되었어서 추가함.)

beforeSend : function() {
	var progressbar = $("#progressbar");
	progressbar.progressbar({
		value: false,
	});
	//progressbar.progressbar("value", 0);
	$('#dialog').dialog({
		modal: true
	});
}

complete에서 jQuery 프로그레스바를 비노출시킨다.

complete/*always*/ : function() {
	$('#dialog').dialog('close');
}

EasyUI 이용

beforeSend에서 EasyUI 프로그레스바를 노출시킨한다.

beforeSend : function(jqXHR, settings) {
	$.messager.progress({
		title:'요청을 처리하는 중입니다.',
		msg:'처리가 끝날 때까지 잠시만 기다려주세요...'
	});
}

complete에서 EasyUI 프로그레스바를 비노출시킨다.

complete/*always*/ : function() {
	$.messager.progress('close');
}

해설

jQuery Ajax에서 beforeSend는 요청 전 항상 실행된다.

또한 complete는 응답을 받고 success와 error 어느 쪽이더라도 그 후에 실행된다.

반응형
728x90

  EasyUI의 [Documentation]을 보면 Properties 항목을 볼 수 있다.
  하지만 Properties 조회 명령어는 properties가 아닌 options이기 때문에 주의가 필요하다.

*예시)

const tab = $('#infoTabs').tabs('getSelected');
const tabOptions = tab.panel('options');
const infoTabId = tabOptions.id;
반응형
728x90

한국투자증권 OpenAPI를 사용하려고 코딩을 했는데, 정작 실행을 하자 [javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]라는 길고 긴 예외가 발생했습니다.

검색을 해보니 원인이 여러가지일 수 있다고 하는군요. 일단 순서대로 첫번째 것부터 진행해봤는데, 첫번째 걸로 해결이 되버렸습니다.

*해결책 링크 : [Java 에서 ValidatorException 등 인증서 관련 에러 해결 - keystore에 SSL/TLS 인증서를 import 하기]

링크의 내용을 그냥 따라하면 되는데, 다만 [6]번에서 keytool이 어디에 있는지 설명이 없어서 약간 해맸습니다. 혹시나 해서 자바 설치 폴더의 bin 폴더를 살펴보니 거기에 있더군요.(설정에 패스가 잡혀 있으면 문제 없겠지만, 저처럼 안 잡혀 있으면 해당 경로에 맞춰서 입력해주면 됩니다.)

덕분에 잘 실행되어서 계속해서 개발해봐야겠습니다.

반응형
728x90

이번에는 @ControllerAdvice입니다. 개인적으로 가장 효율적인 방법이라고 봅니다.


@ControllerAdvice

ControllerAdvice가 모든 컨트롤러에서 발생한 예외를 맡습니다.

예제

<web.xml>

	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/egovframework/springmvc/dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

<dispatcher-servlet.xml>

<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"></bean>

<ControllerAdvice 클래스>

@ControllerAdvice
public class ExampleAdvice extends ResponseEntityExceptionHandler {
	/**
	 * 로그.
	 */
	private static final Log LOG = LogFactory.getLog(ExampleAdvice.class);

	@ExceptionHandler(Throwable.class)
	public String handleException(final Exception e, Model model) {
		LOG.error("Throwable 예외 기록.", e);
		model.addAttribute("msg", "오류 발생 시 메시지 추가 테스트입니다.");
		return "/cmmn/egovError";
	}

	@ExceptionHandler(ExampleForbiddenException.class)
	public String handleException(final ExampleForbiddenException e, Model model) {
		LOG.error("ExampleForbiddenException 예외 기록.", e);
		model.addAttribute("msg", "오류 발생 시 메시지 추가 테스트입니다.");
		return "/cmmn/egovError";
	}
}

장단점 설명

Controller에 Advice를 줄 용도로 만든 클래스에 [@ControllerAdvice]어노태이션을 붙인다. 클래스 내의 오류 처리 메서드에는 [@ExceptionHandler]어노태이션을 붙인다.

[@ExceptionHandler]어노태이션이 붙은 메서드가 동작하게 하려면 [ExceptionHandlerExceptionResolver]를 반드시 설정해야 한다.(Spring 3.1 이후에 DispatcherServlet에 기본으로 설정되어 있다고 하는데, 동작하지 않을 때는 명시적으로 설정하자.)

만약 특정 컨트롤러에서만 독자적인 오류 처리가 필요한 경우, 해당 컨트롤러 내에 예외 처리 메서드를 작성하고 [@ExceptionHandler]어노태이션을 붙이면 된다. ControllerAdvice의 [@ExceptionHandler]보다 컨트롤러 내의 [@ExceptionHandler]가 우선된다.

장점 : 전체 컨트롤러에서 발생하는 오류를 처리해주는 기능을 클래스 하나로 해결할 수 있다. 오류 로그를 남겨도 좋고, 다른 추가 처리를 해도 좋다. 특정 컨트롤러에서만 독자적인 오류 처리가 필요한 경우 분리시킬 수 있는 것도 장점.

단점 : 별달리 없다.

총평 : 가성비가 좋다. -_-)b

참고

반응형
728x90

이번에는 ExceptionResolver 관련 간단한 정리입니다. 미리 준비되어 있는 ExceptionResolver가 몇 종류 있는데, 개인적으로는 ExceptionHandlerExceptionResolver만 쓸 줄 알아도 충분하다고 봅니다.


ExceptionResolver

DispatcherServlet에 ExceptionResolver를 선언하여 에러 발생 시 해당 ExceptionResolver가 처리하게 하는 방식이다.

예제

<web.xml>

	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/egovframework/springmvc/dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

<dispatcher-servlet.xml>

<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"></bean>

<Controller 클래스>

	@ExceptionHandler({ IllegalArgumentException.class, ExampleException.class })
	@ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "Some parameters are invalid")
	public void onBadRequestException(final RuntimeException exception) {
		LOG.error("!오류 핸들러가 실행!", exception);
	}

[ExceptionHandlerExceptionResolver]는 예외 발생 시 [@ExceptionHandler]어노태이션이 붙은 메서드가 처리하게 한다.

장단점 설명

ExceptionResolver 클래스는 추상클래스[AbstractHandlerExceptionResolver]를 상속받거나, 인터페이스[HandlerExceptionResolver]를 구현하여야 한다. 두 경우 모두 메서드[resolveException]가 구현되어야 한다.

메서드[resolveException]은 다음과 같은 특징이 있다.

  • 인자로 Exception을 받는다. 예외를 로그에 남기는 등의 처리가 가능하다.
  • 반환형이 ModelAndView다. 이를 통해 출력할 뷰를 제어할 수 있다.

장점 : 로그를 남기는 등 추가 처리가 가능하다.

단점 : 별달리 없다.

총평 : 미리 지원해주는 ExceptionResolver가 몇 종류 있어서, 알아보고 사용하면 된다. 없으면 직접 상속받거나 구현한 클래스를 사용하면 된다. 다만 개인적으로는 ExceptionHandlerExceptionResolver와 @ControllerAdvice 연계가 가장 가성비가 좋다고 생각한다. @ControllerAdvice에 대해서는 다음에 설명한다.

참고

반응형
728x90

그 전부터 에러페이지 관련하여 한 번 정리하고 싶었는데, 이번에 마침 관련 업무를 할 일이 있었어서 나눠서 정리해보려 합니다.

어노태이션[@ResponseStatus]

예외 클래스 자체에 응답 상태 코드를 선언하여, web.xml의 에 매핑하는 방식이다.

예제

<예외 클래스>

@ResponseStatus(value = HttpStatus.FORBIDDEN)
public class ExampleForbiddenException extends RuntimeException {

	//…이하 생략…
}

위와 같이 예외 클래스에 응답코드를 선언해두고, 처리 로직에서 해당 예외를 던진다.

<처리 클래스>

	@GetMapping(value = "/example/error/throwExampleForbiddenException.do")
	public String throwException(final HttpServletResponse response) {
		if (true) {
			throw new ExampleForbiddenException("@ResponseStatus가 HttpStatus.FORBIDDEN으로 설정되어 있다.");
		}

		return "/example/fakeView"; // 여기는 실행되지 않는다.
	}

<web.xml>

	<error-page>
		<error-code>403</error-code>
		<location>/WEB-INF/jsp/example/error/forbiddenError.jsp</location>
	</error-page>​

장단점 설명

장점 :

  • 쉽고 간단하다. (예외에 응답 상태 코드를 지정하여, 바로 매핑된 에러 페이지를 바로 연결하면 끝)
  • ExceptionResolver가 없어도 동작한다.
  • ExceptionResolver의 종류에 따라서는 @ResponseStatus를 이용하기도 한다.

단점 : 로그 남기기 등의 추가 처리를 일원화하기 어렵다.

총평 : 오류가 나도 해당 정보를 확인할 수 없다는 점 때문에 단독으로 쓰일 일은 별로 없다.

참고

반응형

+ Recent posts