디지털 컨버전스/Project
[Project] include / 상대 경로 / 절대 경로
gimyeondong
2020. 5. 13. 09:32
반복되는 내용이 계속해서 나오는 경우
html 태그가 아니라 JSP 액션태그
<jsp:include page="경로"/>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<jsp:include page="include/header.jsp"/>
Contents
<jsp:include page="include/footer.jsp"/>
</body>
</html>
header.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Header
footer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Footer
setting.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<jsp:include page="include/setting.jsp"/>
</head>
<body>
<jsp:include page="include/header.jsp"/>
Contents
<jsp:include page="include/footer.jsp"/>
</body>
</html>
jQuery
BootStrap cdn 등 저장해놓고 꺼내쓰는 것도 가능
(슬림 제이쿼리는 AJAX 못씀)
다른 경로의 파일을 적용할 경우
member.jsp에 setting.jsp를 적용하고 싶을 때
parent 폴더 지정 : ..
상대 경로 지정 : 내 파일이 있는 위치를 기준으로 찾아가는 경로
이미지 불러오기
index.jsp 에서
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<jsp:include page="include/setting.jsp"/>
</head>
<body>
<jsp:include page="include/header.jsp"/>
Contents
<jsp:include page="include/footer.jsp"/>
<img src="resources/image/rac.jpg">
</body>
</html>
member.jsp에서
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<jsp:include page="../include/setting.jsp"/>
</head>
<body>
<img src="../resources/image/rac.jpg">
</body>
</html>
절대경로
'/'로 시작
프로젝트의 가장 꼭대기에서 부터 경로 지정
<img src="/resources/image/rac.jpg">
안 뜨는 이유 확인 -> 개발자도구> 네트워크> 요청 url 보기
[해결방법 1] 프로젝트 명 넣어주기
<img src="/Day_06_01_include/resources/image/rac.jpg">
프로젝트명을 스트링으로 넣어 놓으면 나중에 프로젝트명 변경시에 수정하기 어려움
[해결방법 2] EL을 통해서 값을 얻어오기
<img src="${pageContext.request.contextPath}/resources/image/rac.jpg">
절대경로로 값을 넣는게 더 편리하다
- 파일 위치 변경
- forward는 url을 바꾸지 않기 때문에 경로에러 생길 수 있음