이번에 작성할 코드는 login.html입니다.
이 페이지는 사용자가 아이디와 비밀번호를 입력해 로그인할 수 있도록 하는 로그인 화면입니다.
입력값을 서버로 전송하기 위해 form 태그를 사용합니다.
잘못된 입력이 있을 경우 오류 메시지가 화면에 표시되도록 구성합니다.
src/main/resources/templates 폴더에 login.html 파일을 추가한 뒤
아래의 코드를 작성합니다.
▼ Code
Path: templates/login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인</title>
<style>
body {
margin: 40px;
}
form {
max-width: 350px;
margin: auto;
}
.row {
display: flex;
margin-bottom: 8px;
}
label {
width: 90px;
text-align: right;
margin-right: 8px;
}
input {
flex: 1;
}
.btns {
text-align: right;
margin-top: 10px;
}
</style>
</head>
<body>
<h2 style="text-align:center;">로그인</h2>
<div th:if="${error}" style="color:red; text-align:center;" th:text="${error}"></div>
<form th:action="@{/login}" method="post">
<div class="row">
<label for="id">ID:</label>
<input type="text" id="id" name="id" required>
</div>
<div class="row">
<label for="password">비밀번호:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="btns">
<button type="submit">로그인</button>
</div>
</form>
</body>
</html>▼ Explain
<style> 화면의 여백과 입력창 모양을 꾸미는 CSS 코드가 포함되어 있습니다. body { margin: 40px; } 문서 본문 전체에 40px 여백을 둡니다. form { max-width: 350px; margin: auto; } 폼의 최대 가로 폭을 350px로 제한합니다. margin: auto는 좌우 자동 여백을 줘서 폼을 가로 중앙에 정렬합니다. .row { display: flex; margin-bottom: 8px; } 각 입력 줄을 flex 컨테이너로 만들어 라벨과 입력창을 한 줄에 배치합니다. margin-bottom: 아래쪽 여백을 8px로 설정합니다. label { width: 90px; text-align: right; margin-right: 8px; } 라벨 폭을 90px로 고정합니다. text-align: 텍스트를 오른쪽으로 정렬합니다. margin-right: 오른쪽 여백을 8px로 설정합니다. input { flex: 1; } 입력창을 가변 폭으로 확장합니다. Flex 컨테이너에서 남는 공간을 모두 차지해 폼 폭에 자연스럽게 맞춥니다. 창 크기가 달라져도 사용성이 유지됩니다. .btns { text-align: right; margin-top: 10px; } 버튼 묶음을 오른쪽 정렬합니다. 위쪽에 10px 여백을 줍니다.
▼ Explain
<div th:if="${error}" style="color:red; text-align:center;" th:text="${error}"></div> 로그인 실패 시 오류 메시지를 보여주는 영역입니다. th:if="${error}" 서버에서 전달된 error 라는 이름의 변수값이 있을 때만 이 문장을 표시합니다. th:text="${error}" 전달된 오류 메시지를 텍스트로 출력합니다. <form th:action="@{/login}" method="post"> 로그인 정보를 서버로 보낼 폼(form) 부분입니다. th:action="@{/login}" 사용자가 입력한 데이터를 http://localhost/login 주소로 보낸다는 뜻입니다. method="post" POST 방식으로 전송하여 보안성을 높입니다. <div class="row"> <label for="id">ID:</label> <input type="text" id="id" name="id" required> </div> 아이디를 입력하는 영역입니다. <label for="id"> id="id" 입력창과 연결됩니다. required 필수 입력 항목을 의미합니다. 값을 입력하지 않으면 폼이 제출되지 않습니다. <div class="row"> <label for="password">비밀번호:</label> <input type="password" id="password" name="password" required> </div> 비밀번호 입력영역입니다. type="password" 입력한 글자가 화면에 노출되지 않고 마스킹 처리됩니다. <div class="btns"> <button type="submit">로그인</button> </div> 로그인 버튼 영역입니다. 버튼을 누르면 폼에 입력된 아이디와 비밀번호가 /login 주소로 전송됩니다.
개인정보처리방침 · 이용약관