처음 만드는 Spring Boot 메모장

list.html 메모 작성 버튼

· 1 views

사용자가 메모를 새로 작성할 수 있도록,
메모 목록 페이지인 list.html에 "메모 작성" 버튼을 추가합니다.
이 버튼을 클릭하면 /write 경로로 이동하여 메모 입력 화면으로 전환됩니다.

▼ Code

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>메모 목록</title>
<link rel="stylesheet" href="/css/main.css"​>
</head>
<body>
    <h1>메모 목록</h1>
    <button type="button" onclick="location.hr​ef='/write'">메모 작성</button>
    <table border="1">
        <tr>
            <th>번호</th>
            <th>제목</th>
            <th>작성일</th>
        </tr>
        <tr th:each="memo, stat : ${memos}">
          <td th:text="${stat.coun​t}">번호</td>
          <td th:text="${memo.titl​e}">제목</td>
          <td th:text="${#temporal​s.format(memo.reg_da​te, 'yyyy-MM-dd HH:mm:ss')}">작성일</td​>
        </tr>
    </table>
</body>
</html>

▼ Explain

type="button"
이 버튼은 폼 전송(submit) 기능과는 무관한 일반 버튼입니다.
따라서 버튼을 클릭해도 기본 동작은 없습니다.
실제 동작을 수행하려면 JavaScript를 이용한 동작 처리가 필요합니다.

onclick="location.hr​ef='/write'"
버튼 클릭 시 실행될 JavaScript 코드입니다.
location.href = '/write'는 현재 페이지를 /write 경로로 이동시킵니다.
결과적으로 메모 작성 페이지로 이동하는 버튼 기능이 됩니다.

댓글

아직 댓글이 없습니다.