Skip to content

fix(sample): 컨텍스트 패스 하위 배포 시 폼 액션이 루트로 만들어지는 문제 수정 - #78

Merged
jei007 merged 1 commit into
eGovFramework:mainfrom
wantaekchoi:fix/context-path-holder-missing
Sep 11, 2026
Merged

fix(sample): 컨텍스트 패스 하위 배포 시 폼 액션이 루트로 만들어지는 문제 수정#78
jei007 merged 1 commit into
eGovFramework:mainfrom
wantaekchoi:fix/context-path-holder-missing

Conversation

@wantaekchoi

Copy link
Copy Markdown
Contributor

수정 사유 Reason for modification

  • 버그수정 Bug fixes
  • 기능개선 Enhancements
  • 기능추가 Adding features
  • 기타 Others

수정된 소스 내용 Modified source

common.js 첫 줄이 컨텍스트 패스를 DOM 에서 읽습니다.

$ git grep -n 'contextPathHolder' origin/main
origin/main:src/main/resources/static/js/egovframework/common.js:1:const contextPath = $('#contextPathHolder').attr('data-contextPath') ? $('#contextPathHolder').attr('data-contextPath') : '';

#contextPathHolder 는 저장소 전체에서 이 한 줄에만 나옵니다. 그 엘리먼트를 렌더하는 템플릿이 없어 선택자가 아무것도 잡지 못하고, 값은 늘 '' 입니다.

이 변수는 egovSampleList.html(16·24·29·34줄)과 egovSampleRegister.html(31·40·51·58줄)의 폼 액션 여덟 군데에 그대로 앞에 붙습니다. 검색·페이징·등록화면·상세·목록·등록·수정·삭제 버튼이 만드는 URL 이 전부 루트 기준이 됩니다.

같은 파일의 <head> 는 CSS·JS 를 이미 th:href="@{/css/egovframe.css}" 처럼 걸고 있습니다. 한 파일 안에서 URL 을 만드는 방식이 둘로 갈리고, 그중 하나만 컨텍스트 패스를 놓칩니다.

v5.0.0 직전 릴리스인 v4.3.0(89724ea)까지는 두 템플릿이 <head> 에서 홀더를 직접 넣었습니다. 홀더를 지운 커밋은 v5.0.0 임포트(95b2f01) 하나뿐입니다.

$ git grep -n 'contextPathHolder' v4.3.0
v4.3.0:src/main/resources/templates/thymeleaf/egovSampleList.html:7:    <link id="contextPathHolder" th:data-contextPath="${#httpServletRequest.getContextPath()}"/>
v4.3.0:src/main/resources/templates/thymeleaf/egovSampleList.html:17:        var contextPath = $('#contextPathHolder').attr('data-contextPath') ? $('#contextPathHolder').attr('data-contextPath') : '';
v4.3.0:src/main/resources/templates/thymeleaf/egovSampleRegister.html:8:	<link id="contextPathHolder" th:data-contextPath="${#httpServletRequest.getContextPath()}"/>
v4.3.0:src/main/resources/templates/thymeleaf/egovSampleRegister.html:18:		var contextPath = $('#contextPathHolder').attr('data-contextPath') ? $('#contextPathHolder').attr('data-contextPath') : '';

v5.0.0(95b2f01)에서 읽는 쪽만 common.js 로 옮겨지고, 넣는 쪽이 함께 옮겨지지 않았습니다.

홀더를 그대로 되살리지는 않았습니다. ${#httpServletRequest} 는 Thymeleaf 3.1 에서 없어진 표현식이고(이 빌드가 받는 버전은 org.thymeleaf:thymeleaf:jar:3.1.3.RELEASE), 두 파일에는 정적 자원이 이미 쓰는 @{...} 가 있습니다.

변경

폼 액션도 정적 자원과 같은 방식으로 만듭니다.

-$('#listForm').attr('method', 'get').attr('action', contextPath + '/egovSampleList.do').submit();
+$('#listForm').attr('method', 'get').attr('action', [[@{/egovSampleList.do}]]).submit();

나머지 일곱 군데도 같은 모양입니다. DOM 을 거치지 않게 되면서 참조가 없어진 common.jscontextPath 선언은 함께 지웠습니다.

영향 범위

목록·등록 두 화면의 폼 액션 여덟 군데입니다. 컨텍스트 패스가 없는 기본 배포에서는 만들어지는 URL 이 종전과 같습니다(아래 18081 은 컨텍스트 패스 없이 따로 띄운 인스턴스입니다).

$ curl -s http://localhost:18081/egovSampleList.do | grep -nE "attr\(.action"
16:				$('#listForm').attr('method', 'get').attr('action', "\/egovSampleList.do").submit();
24:        	$('#listForm').attr('method', 'get').attr('action', "\/egovSampleList.do").submit();
29:        	$('#listForm').attr('method', 'post').attr('action', "\/addSampleView.do").submit();
34:            $('#listForm').attr('method', 'post').attr('action', "\/updateSampleView.do").submit();

common.js 의 전역 선언 삭제는 공용 파일 변경입니다. 저장소 안 참조는 위 여덟 군데가 전부라 이번 변경으로 0 이 되지만, 이 샘플을 복사해 자기 화면을 만들면서 그 전역을 쓰고 있다면 그쪽도 @{...} 로 함께 바꿔야 합니다.

JUnit 테스트 JUnit tests

  • JUnit 테스트 JUnit tests
  • 수동 테스트 Manual testing

가설은 "컨텍스트 패스 /sample 로 두 화면을 렌더하면 화면에 나타난 *.do 경로가 모두 /sample 로 시작해야 한다" 입니다. EgovSampleControllerTestContextPathTest 가 경로별로 등장 횟수와 /sample 이 붙은 등장 횟수를 대조합니다.

$ mvn test

수정 전

[ERROR] Failures: 
[ERROR]   EgovSampleControllerTestContextPathTest.test_등록화면_폼액션에_컨텍스트패스가_붙는다:45->assertContextPath:63 /egovSampleList.do 폼 액션에 컨텍스트 패스가 빠졌다 ==> expected: <1> but was: <0>
[ERROR]   EgovSampleControllerTestContextPathTest.test_목록화면_폼액션에_컨텍스트패스가_붙는다:36->assertContextPath:63 /egovSampleList.do 폼 액션에 컨텍스트 패스가 빠졌다 ==> expected: <2> but was: <0>
[ERROR] Tests run: 56, Failures: 2, Errors: 0, Skipped: 0

수정 후

[INFO] Tests run: 56, Failures: 0, Errors: 0, Skipped: 0

테스트 브라우저 Test Browser

  • Chrome
  • Firefox
  • Edge
  • Safari
  • Opera
  • Internet Explorer
  • 기타 Others

브라우저 대신 server.servlet.context-path=/sample 로 실제 기동해 응답 본문을 확인했습니다.

테스트 스크린샷 또는 캡처 영상 Test screenshots or captured video

수정 전 — 정적 자원에만 /sample 이 붙고 폼 액션에는 붙지 않습니다.

$ curl -s http://localhost:18080/sample/egovSampleList.do | grep -nE "<script|<link rel|\.do"
9:	<link rel="stylesheet" type="text/css" href="/sample/css/component/output.css"/>
10:	<link rel="stylesheet" type="text/css" href="/sample/css/egovframe.css"/>
11:	<script src="/sample/js/jquery.min.js"></script>
12:	<script src="/sample/js/egovframework/common.js" defer></script>
13:	<script defer>
16:				$('#listForm').attr('method', 'get').attr('action', contextPath + '/egovSampleList.do').submit();
24:        	$('#listForm').attr('method', 'get').attr('action', contextPath + '/egovSampleList.do').submit();
29:        	$('#listForm').attr('method', 'post').attr('action', contextPath + '/addSampleView.do').submit();
34:            $('#listForm').attr('method', 'post').attr('action', contextPath + '/updateSampleView.do').submit();

$ curl -s http://localhost:18080/sample/egovSampleList.do | grep -c contextPathHolder
0

$ curl -s -o /dev/null -w "%{http_code}
" http://localhost:18080/egovSampleList.do   # JS 가 만드는 경로
404
$ curl -s -o /dev/null -w "%{http_code}
" http://localhost:18080/sample/egovSampleList.do   # 실제 매핑
200

수정 후

$ curl -s http://localhost:18080/sample/egovSampleList.do | grep -nE "<script|<link rel|\.do"
9:	<link rel="stylesheet" type="text/css" href="/sample/css/component/output.css"/>
10:	<link rel="stylesheet" type="text/css" href="/sample/css/egovframe.css"/>
11:	<script src="/sample/js/jquery.min.js"></script>
12:	<script src="/sample/js/egovframework/common.js" defer></script>
13:	<script defer>
16:				$('#listForm').attr('method', 'get').attr('action', "\/sample\/egovSampleList.do").submit();
24:        	$('#listForm').attr('method', 'get').attr('action', "\/sample\/egovSampleList.do").submit();
29:        	$('#listForm').attr('method', 'post').attr('action', "\/sample\/addSampleView.do").submit();
34:            $('#listForm').attr('method', 'post').attr('action', "\/sample\/updateSampleView.do").submit();

common.js 는 #contextPathHolder 엘리먼트의 data-contextPath 를 읽어
contextPath 변수를 만들고, 목록·등록 화면의 폼 액션 8군데가 이 값을 앞에
붙인다. 그런데 그 엘리먼트를 렌더하는 템플릿이 없어 값이 늘 '' 이다.
v4.3.0 까지는 두 템플릿이 head 에 <link id="contextPathHolder"> 를 직접
넣었는데, v5.0.0 에서 읽는 쪽만 common.js 로 옮기면서 넣는 쪽이 빠졌다.

server.servlet.context-path=/sample 로 띄우면 CSS·JS 는 @{...} 덕에
/sample 이 붙지만 검색·페이징·등록·상세·수정·삭제·목록 버튼은
/egovSampleList.do 로 제출돼 404 가 난다.

같은 파일이 정적 자원에 이미 쓰고 있는 @{...} 로 액션 URL 을 만들어 DOM
왕복을 없앤다. 쓰는 곳이 없어진 common.js 의 contextPath 선언도 지운다.
컨텍스트 패스가 없는 기본 배포에서는 만들어지는 URL 이 종전과 같다.
@wantaekchoi
wantaekchoi force-pushed the fix/context-path-holder-missing branch from ea78473 to 850ac43 Compare September 10, 2026 02:37

@jei007 jei007 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

표준프레임워크에 대한 지속적인 참여에
대단히 감사드립니다.

@jei007
jei007 merged commit ea068f8 into eGovFramework:main Sep 11, 2026
1 check passed
@wantaekchoi
wantaekchoi deleted the fix/context-path-holder-missing branch September 11, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants