📝 프로필 수정 미리보기 기능
💡 VS Code
🔎 myPage-profile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- 문자열 관련 함수(메소드) 제공 JSTL (EL 형식으로 작성) --%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Page</title>
<link rel="stylesheet" href="${contextPath}/resources/css/main-style.css">
<link rel="stylesheet" href="${contextPath}/resources/css/myPage-style.css">
<script src="https://kit.fontawesome.com/4dca1921b4.js" crossorigin="anonymous"></script>
</head>
<body>
<main>
<jsp:include page="/WEB-INF/views/common/header.jsp"/>
<section class="myPage-content">
<jsp:include page="/WEB-INF/views/member/sideMenu.jsp"/>
<section class="myPage-main">
<h1 class="myPage-title">프로필</h1>
<span class="myPage-explanation">프로필 이미지를 변경할 수 있습니다.</span>
<form action="#" method="POST" name="myPage-form">
<div class="profile-image-area">
<img src="${contextPath}/resources/images/user.png" id="profile-image">
</div>
<div class="profile-btn-area">
<label for="input-image">이미지 선택</label>
<input type="file" name="profileImage" id="input-image">
<button type="submit">변경하기</button>
</div>
<div class="myPage-row">
<label>이메일</label>
<span>${loginMember.memberEmail}</span>
</div>
<div class="myPage-row">
<label>가입일</label>
<span>${loginMember.enrollDate}</span>
</div>
</form>
</section>
</section>
</main>
<jsp:include page="/WEB-INF/views/common/footer.jsp"/>
<script src="${contextPath}/resources/js/member/myPage.js"></script>
</body>
</html>
🔎 myPage-style.css
...
.profile-image-area{
width: 150px;
height: 150px;
border: 3px solid #ddd;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
#profile-image{
width: 100%;
height: 100%;
border-radius: 50%;
}
.profile-btn-area{
width: 230px;
margin: 20px 0;
display: flex;
align-items: center;
justify-content: space-around;
}
.profile-btn-area > *{
width: 110px;
height: 33px;
padding: 5px 10px;
border: 1px solid black;
background-color: white;
font-size: 14px;
cursor: pointer;
text-align: center;
}
#input-image{
display: none;
}
.profile-btn-area > button{
background-color: #455ba8;
color: white;
border: 0;
}
🔎 index.jsp
...
<%-- 로그인이 되어 있는 경우 --%>
<c:otherwise>
<article class='login-area'>
<a href="${contextPath}/member/myPage/profile">
<img src="/community/resources/images/user.png" id="member-profile">
</a>
...
...
<section class="left-side">
사이드 메뉴
<ul class="list-group">
<li> <a href="${contextPath}/member/myPage/profile">프로필</a> </li>
...
🔎 myPage.js
const inputImage = document.getElementById("input-image");
if(inputImage != null){
inputImage.addEventListener("change", function(){
if(this.files[0] != undefined){
const reader = new FileReader();
reader.readAsDataURL(this.files[0]);
reader.onload = function(e){
const profileImage = document.getElementById("profile-image");
profileImage.setAttribute("src", e.target.result);
}
}
});
}